Skip to content

Commit 16c377e

Browse files
committed
Properly parsing m3u/m3u8 files
Fix reconnecting to stream
1 parent c14d4f9 commit 16c377e

3 files changed

Lines changed: 50 additions & 16 deletions

File tree

QuickViewFile/Controls/VideoPlayerControl.xaml.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ protected virtual void Dispose(bool disposing)
269269
}
270270
}
271271

272+
// Ta metoda wywoła się, gdy stream się "urwie" lub skończy
272273
private void VideoInWindowPlayer_MediaEnded(object sender, RoutedEventArgs e)
273274
{
274-
// Stream się zakończył (np. serwer zerwał połączenie) - próbujemy połączyć ponownie
275275
ReloadStream();
276276
}
277277

@@ -281,23 +281,18 @@ private void VideoInWindowPlayer_MediaFailed(object sender, ExceptionRoutedEvent
281281
ReloadStream();
282282
}
283283

284-
private void ReloadStream()
284+
private async void ReloadStream()
285285
{
286-
// Check if user paused video manually
287-
if (isVideoPaused || !mediaPlayerIsPlaying) return;
286+
if (isVideoPaused) return;
288287

289-
Dispatcher.Invoke(() =>
290-
{
291-
// This will force WPF to reconnect to the stream
292-
var currentSource = videoInWindowPlayer.Source;
288+
await Task.Delay(1);
293289

294-
if (currentSource != null)
295-
{
296-
videoInWindowPlayer.Source = null;
297-
videoInWindowPlayer.Source = currentSource;
298-
videoInWindowPlayer.Play();
299-
}
300-
});
290+
if (isVideoPaused) return;
291+
292+
var currentTempSource = videoInWindowPlayer.Source;
293+
videoInWindowPlayer.Source = null;
294+
videoInWindowPlayer.Source = currentTempSource;
295+
videoInWindowPlayer.Play();
301296
}
302297

303298
public TimeSpan GetCurrentVideoPosition()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,46 @@
1+
using System.IO;
2+
13
namespace QuickViewFile.Helpers
24
{
35
public static class FileContentReader
46
{
7+
/// <summary>
8+
/// Reads file m3u/m3u8 and parse valid stream URL
9+
/// </summary>
10+
public static string? ExtractStreamUrlFromM3u(string filePath)
11+
{
12+
if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath))
13+
return null;
14+
15+
try
16+
{
17+
foreach (string line in File.ReadLines(filePath))
18+
{
19+
if (string.IsNullOrWhiteSpace(line))
20+
continue;
21+
22+
string cleanLine = line.Trim();
23+
24+
if (cleanLine.StartsWith("#"))
25+
continue;
26+
27+
28+
if (cleanLine.StartsWith("http", StringComparison.OrdinalIgnoreCase) || // both http and https
29+
cleanLine.StartsWith("rtsp", StringComparison.OrdinalIgnoreCase) ||
30+
cleanLine.StartsWith("rtmp", StringComparison.OrdinalIgnoreCase) ||
31+
cleanLine.StartsWith("udp", StringComparison.OrdinalIgnoreCase) ||
32+
cleanLine.StartsWith("mms", StringComparison.OrdinalIgnoreCase))
33+
{
34+
return cleanLine;
35+
}
36+
}
37+
}
38+
catch (Exception ex)
39+
{
40+
return "Error parsing m3u/m3u8 file!";
41+
}
542

43+
return null;
44+
}
645
}
746
}

QuickViewFile/ViewModel/FilesListViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public async Task LazyLoadFile(bool? forceLoad = false)
249249
{
250250
try
251251
{
252-
string streamUrl = File.ReadAllText(filePath).Trim();
252+
string streamUrl = FileContentReader.ExtractStreamUrlFromM3u(filePath);
253253
SelectedItem.FileContentModel.VideoMedia = new Controls.VideoPlayerControl(streamUrl, Config.BitmapScalingMode);
254254
SelectedItem.FileContentModel.TextContent = null;
255255
SelectedItem.FileContentModel.ShowTextBox = false;

0 commit comments

Comments
 (0)