-1

I have created a UWP app to play certain tracks in the background. Basically by following this link: https://blogs.windows.com/buildingapps/2016/01/13/the-basics-of-background-audio/ . I want to set the repeat count for certain songs, so if a song has repeat count 10, that song is meant to be repeated 10 times before moving on to the next song in the playlist.

On the Windows phone 8.0 platform, the AudioPlayerAgent had the following event which indicated that the play state has changed. It was easy to override that event and add custom logic to repeat songs.

protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
{
    switch (playState)
    {
        case PlayState.TrackEnded:

            // keep repeating the same track
            player.Position = new TimeSpan(0, 0, (int)0);
            // add custom logic here..    
            break;
    }
    NotifyComplete();
}

What would be an equivalent event in the UWP platform? So far I have tried the following events on the UWP platform, but to no avail..

BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged;
BackgroundMediaPlayer.Current.MediaEnded += Current_MediaEnded;
BackgroundMediaPlayer.Current.MediaOpened += Current_MediaOpened;
bit
  • 4,407
  • 1
  • 28
  • 50

1 Answers1

-1

With Windows 10, version 1607, a new single-process model has been introduced that greatly simplifies the process of enabling background audio.

Media continues to play when your app moves from the foreground to the background. This means that even after the user has minimized your app, returned to the home screen, or has navigated away from your app in some other way, your app can continue to play audio.

Starting with Windows 10, version 1607,the recommended best practice for playing media is to use the MediaPlayer class instead of MediaElement

Play a media file with MediaPlayer

_mediaPlayer = new MediaPlayer();
_mediaPlayer.Source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/example_video.mkv"));
_mediaPlayer.Play();

MSDN: Play media in the background

Now your app can manage the playlist or loop settings and use the media player instance to call Play method again.

Shubhan
  • 874
  • 7
  • 13
  • 1
    I don't see how that allows you to repeat a single track for a desired number of times.. – bit Aug 11 '16 at 11:15
  • Since the background audio is now within the actual app, the app will be able to call Play method again. This can be done using the PlaybackSession object's Position property in the Media ended event. (Note: you now need to use the new MediaPlayer class not backgroundaudioplayer class) – Shubhan Aug 12 '16 at 06:09