locked
MediaElement.SetSource() only starts audio if delay of 1000ms added RRS feed

  • Question

  • When streaming audio from a DLNA source the audio won't start if the wma file is opened and immediately set to the MediaElement source.

    Here is the code:

    var stream = await songFile.OpenReadAsync();
    await Task.Delay(1500); //Without this delay audio will not start
    this.player.SetSource(stream, songFile.ContentType);

    Here is the XAML:

    <MediaElement x:Name="player" AutoPlay="True" AudioCategory="BackgroundCapableMedia" AreTransportControlsEnabled="True" CurrentStateChanged="player_CurrentStateChanged" MediaEnded="player_MediaEnded"/>

    I don't want to introduce this delay. Any ideas how this can be done?

    Sunday, January 5, 2014 6:39 PM

Answers

  • Hi pylon,

    Without 1500ms delay, the audio will not start. I guess that the file is big or the network transfer rate is not good enough(you mentioned the source is from DLNA) and the app may take nearly 1500ms to load the file stream to memory, if you work with a local storage file, your code should be great.

    I would suggest you to use a ProgressBar or ProgressRing for the status monitoring instead of Task.Delay(). You could download that file to local and open it by MediaElement, at least your clients should know that the app is downloading something but not just wait without any information notified.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by pylon Monday, January 6, 2014 3:34 PM
    Monday, January 6, 2014 5:37 AM
    Moderator