0

I'm implementing a background audio Windows 8 Store App with C#/XAML, and I managed to run audio in the background, using a MediaElement with BackgroundCapableMedia. According to that blog post, I should be able to play playlists.

However when the track is finished, I can't find how to move to the next track. If I use mediaElement.MediaEnded, the event handler is not called when the application is in the background.

George
  • 36,413
  • 9
  • 66
  • 103
Flavien
  • 7,497
  • 10
  • 45
  • 52

1 Answers1

0

Basically you have to make your own playlist and implement the logic yourself. Your Playlist can be just a collection and you get the next track in the collection and play it. An example from my code:

internal ObservableCollection<StoryViewModel> Playlist { get; set; }

void me_MediaEnded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
    Debug.WriteLine("me_MediaEnded");

    // Zero or one tracks only, so just end...
    if (Playlist.Count <= 1)
    {
        return;
    }
    else
    {
        // We have more tracks we need to play
        Playlist.RemoveAt(0);
        PlayPlaylist();
    }
}

internal void PlayPlaylist()
{
    if (Playlist.Count == 0)
        return;

    // Set the MediaControls metadata
    MediaControl.ArtistName = _svm.ProgramTitle ?? "NPR";
    MediaControl.TrackName = _svm.Title;

    // This centralized dispatcher object is updated by each page to ensure it is current
    _dispatcher.RunAsync(
        CoreDispatcherPriority.Normal, () =>
        {
            // Set the MediaElement to the audio and play
            Me.Source = _svm.Mp3Uri;
            Me.Play();
        });
}
Ray Ackley
  • 390
  • 4
  • 9
  • Maybe I'm doing something wrong, but like I mentioned, that doesn't work for me because the MediaEnded function is not called unless the application is in the foreground. – Flavien Nov 29 '12 at 17:05