BackgroundAudioTask not working after cancelled by another app

391 Views Asked by At

When the BackgroundAudioTask for my app is cancelled by other app on Windows Phone 8.1 which also uses BackgroundAudioTask, when I go back into my app, it will no longer play audio in the background. It will play fine when the app is running but if it is suspended - the background audio also stops.

The steps to reproduce this issue are:
I launch the Windows Phone 8.1 app which has a BackgroundAudioTask & everything works fine. I that start another app, for example the Music player, that uses a BackgroundAudioTask it will cancel the BackgroundAudioTask of my app.

When I launch my app for the second time, I want to re-register my BackgroundAudioTask so that it will behave as it did originally.

In Package.appxmanifest I have the following:

<Extensions>
    <Extension Category="windows.backgroundTasks" EntryPoint="WindowsPhoneBackgroundAudioTask.BackgroundAudioTask">
      <BackgroundTasks>
        <Task Type="audio" />
      </BackgroundTasks>
    </Extension>
  </Extensions>

When I first run the application the Run method will be called and I add a Deferral to the task to make sure it is kept alive even when I close my application:

public void Run(IBackgroundTaskInstance taskInstance)
{
    setupDeferral = taskInstance.GetDeferral();
} 

When I start the music player from another application my BackgroundAudioTask Cancelled event is called (If I don't do setupDeferral.Complete() here my application will crash):

private void Task_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
    setupDeferral.Complete();
}

When I open my app how do I recreate my BackgroundAudioTask? The problem is the Run method is never called again so I can't setup the Deferral again. Music will now play fine in the app, but as soon as I navigate away from the app the music will stop.

I tried manually re-registering the task in App.xaml.cs in the App_Resuming event with this code:

var taskBuilder = new BackgroundTaskBuilder();
taskBuilder.Name = "BackgroundAudioTask";
taskBuilder.TaskEntryPoint = typeof(WindowsPhoneBackgroundAudioTask.BackgroundAudioTask).FullName;
BackgroundTaskRegistration task = taskBuilder.Register();

The above code will throw and InvalidArgumentException because it does not have a Trigger setup. I don't want it to have a trigger. I just want to start the background task immediately.

Is there a way to manually instruct the OS to run the background audio again or a way to handle cancelled background audio better?

2

There are 2 best solutions below

0
On

I know this is really old - but maybe someone will come across it. You actually don't need to register the task at all. In the case of background audio, all you need to do is call

BackgroundMediaPlayer.Current

And it will fire up the task, and then your code will get the deferral.

0
On

I too had this same issue. The Background Audio Task wasn't starting playback once it was cancelled - either due to 5 minutes of inactivity, or due to another app. I was referring the sample code given by Microsoft here.

After hours of searching on the internet, I didn't find a solution. Then, digging in my code further, I found out that when the task is cancelled, the BackgroundMediaPlayer.Current.CurrentState becomes MediaPlayerState.Closed.

Hence, in order to restart the task/background audio playback, just set a source to the BackgroundMediaPlayer.Current again. In the sample code, this media player object is referenced using a variable named mediaPlayer inside the PlaylistManager project component.

Although the sample has a piece of code to restart playback once the task is cancelled, it does not work.