silverlightwindows-phone-8background-audio

Cannot get Background Audio work in WP81 Silverlight


I am migrating a Windows Phone 7.8 application from to Windows Phone 8.1 Silverlight - and ultimately possibly to Windows Phone 10..

But now I am stuck in a triviality of not getting Background Audio Player to work in Windows Phone 8.1 Silverlight at all. This probably is something obvious - but after a few days of experimentation I am running out of ideas.

What happens is that I get error message

System.InvalidOperationException: E_FAIL -2146233079

from player when setting audio track.

Essential code snippets

public partial class MainPage : PhoneApplicationPage
{
    BackgroundAudioPlayer player;
    Uri _trackUri;
    Uri _albumArtUri;
    string _trackUriString = "http://podcast.cbc.ca/mp3/hourlynews.mp3";
    string _albumArtUriString = "http://www.cbc.ca/podcasting/images/promo-hourlies.jpg";
    string _trackAlbum = "Album - Podcast news";
    string _trackArtist = "Artist - CBC";
    string _trackTitle = "Track title - Hourly news";
    string _tag = "testtag";
    public MainPage()
    {
        InitializeComponent();
    }
    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        player = BackgroundAudioPlayer.Instance;
        _trackUri = new Uri(_trackUriString, UriKind.Absolute);
        _albumArtUri = new Uri(_albumArtUriString, UriKind.Absolute);
    }
    private void SetTrack_button_Click(object sender, RoutedEventArgs e)
    {
        player.Track = createTrack();       // player gets error after this statement
    }
    private AudioTrack createTrack()
    {
        return new AudioTrack(_trackUri, _trackTitle, _trackArtist, _trackAlbum, _albumArtUri, _tag, EnabledPlayerControls.All);
    }

I created this in VisualStudio 2015 Community solution with two Silverlight 8.1 projects using the built in templates. And yes, background audio project is referenced from main program. link to project

To check my sanity I created - in a similar way - a WP7.8 project (link to project) using VS12 Express. And copied the code over from VS15 side. That application works fine.

Ideas appreciated - very much


Solution

  • Okay, finally got into looking into this question more.. And there it was.. The answer.. Plain and simple: Not supported this way.. In this link they say

    "AudioPlayerAgent and AudioStreamingAgent not supported for Silverlight 8.1 apps

    The AudioPlayerAgent and AudioStreamingAgent classes, which supported background audio playback for Windows Phone 8 apps, are not supported in Silverlight 8.1. If you want to support background audio playback, you can continue to use a Windows Phone 8 app or create a Windows Phone Store app, which supports new background audio APIs."

    So that is it.. :(