简体   繁体   中英

How to live stream a .m3u8 video file in Xamarin.Forms

(New to Xamarin.Forms) I want to livestream.m3u8 video files in my Xamarin.Forms application?

This is from Microsoft's Documentation

"No stream option is available for the VideoPlayer however, because iOS and Android do not support playing a video from a stream."

[it's from the paragraph right above 'Video Sources' Heading]

Is it possible? What are my options?

This is the first time I needed to play a video, I searched for some libraries and found Xamarin.MediaManager but it was throwing an exception on iOS (On Initializing the video player) and was not playing m3u8 files on Android either (it was playing mp3 and mp4 files with https urls)

So I decided to use the custom renderers for videoplayer given in Xamarin.Forms Documentation

  • Now I can play m3u8 files from a url
  • Even play http urls on iOS (had ATS issues before)
  • but cannot play live streams

This is the VideoPlayer Demo/Tutorial I followed in addition to the Documentation Linked Above.

The VideoPlayer in XAML

        <video:VideoPlayer VerticalOptions="FillAndExpand"
                           HorizontalOptions="FillAndExpand"
                           BackgroundColor="Black"
                           HeightRequest="200"
                           Margin="0,0,0,0"
                           x:Name="FormsVideoPlayer"/>

This is how I am setting the source

                FormsVideoPlayer.Source = new UriVideoSource
                {
                    Uri = url
                };

                FormsVideoPlayer.Play();

Found the solution:

I was getting the video URLs for live streams by generating them on my Laptop. The service I was using was somehow binding (assumption) them with the PC.

I don't know how but the videos could only be played on the Laptop from which the URL was generated (found this after I added a UWP project to the app)

=>> Once I started generating the URLs from within the app itself, the videos started working.

Now I am using CrossMediaManager for HLS videos using the code below:

        if (isHLS)
        {
            var item = await CrossMediaManager.Current.MediaExtractor.CreateMediaItem(URL);

            item.MediaType = MediaManager.Media.MediaType.Hls;
            CrossMediaManager.Current.MediaPlayer.VideoView.ShowControls = false;

            await CrossMediaManager.Current.Play(item);
        }
        else
        {
            await CrossMediaManager.Current.Play(URL);
        }

Xaml:

<ContentPage.Content>
    <mm:VideoView VerticalOptions="FillAndExpand"
                  HorizontalOptions="FillAndExpand"
                  BackgroundColor="Black"
                  x:Name="mmVideoView"
                  />
</ContentPage.Content>

Just in case someone faces a similar problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM