0

I am trying to assign a HLS stream to a MediaElement and then play it immediately but I'm failing to do so.

It's an UWP application (Windows 10).

Uri hlsUri = new Uri("http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8");
AdaptiveMediaSourceCreationResult hlsSource = await AdaptiveMediaSource.CreateFromUriAsync(hlsUri);

if (hlsSource .Status == AdaptiveMediaSourceCreationStatus.Success)
{
    videoPlayer.SetMediaStreamSource(hlsSource .MediaSource);
    videoPlayer.Play();
}

For some reason this thing doesn't work. The StreamSource is set, but for some reason the Play() is executed before that.

I tried to wrap the CreateFromUriAsync method in a Task, like suggested here, to wait for the result first, but I couldn't make it work.

It works with await Task.Delay(TimeSpan.FromMilliseconds(400));, but that looks to me like a ticking timebomb. However, it leads me to believe, that my problem is somewhere in the async method.

Community
  • 1
  • 1
Stefan
  • 73
  • 8

1 Answers1

1

If you want to inmediately start playing after the MediaElement is set, I would suggest you to put its AutoPlay property to true. This should work! :)

Eva FP
  • 775
  • 9
  • 24
  • Well, i guess that should work. It's still strange, that `Play()` is executed before the setting of the Media Source. – Stefan Sep 21 '16 at 16:02
  • 1
    That"s because the creation of the `AdaptiveMediaSource` is asynchronous and awaited. Another thing you could is running the creation in a thread and wait until it finishes, for example using `Task.Run()` but I think it doesn't make much sense – Eva FP Sep 21 '16 at 16:08
  • 1
    @Stefan please mark the answer as the solution if it has solved your issue :) – Eva FP Sep 22 '16 at 07:35
  • Sure. It's a workaround, but it really is the easiest way out of this sticky situation. :) – Stefan Sep 26 '16 at 08:54