BackgroundAudioTask does not work after canceled by another application

When an app BackgroundAudioTask

for my app is canceled by another app on Windows Phone 8.1 that is also using BackgroundAudioTask

, when I return to my app, it will no longer play audio in the background. It will play fine when the app is launched, but if it is paused, the background sound will also stop.

Steps to reproduce this issue:
I run the Windows Phone 8.1 app that I have BackgroundAudioTask

and everything works fine. I am starting another application like the music player that is using BackgroundAudioTask

it will cancel BackgroundAudioTask

my application.

When I run my application a second time, I want to re-register mine BackgroundAudioTask

so that it behaves the way it originally did.

In Package.appxmanifest I have the following:

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

      

The Run method will be called the first time the application is launched and I will add Deferral to the task to make sure it is kept alive even when I close the application

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

      

When I start the music player from another app my BackgroundAudioTask Canceled event is called (if I don't do setupDeferral.Complete()

here my app will crash):

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

      

When I open my app, how do I recreate mine BackgroundAudioTask

? The problem is that the method is Run

never called again, so I can't set it up again Deferral

. The music will now play well in the app, but as soon as I leave the app, the music will stop.

I tried to manually re-register 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 issue and InvalidArgumentException

because it has no Trigger setting. I don't want him to have a trigger. I just want to run the background job right away.

Is there a way to manually instruct the OS to start the background sound again, or is it better to handle the canceled background sound?

+3


source to share


2 answers


I know this is really old - but maybe someone will run into this. You don't need to register the issue at all. In the case of background audio, all you have to do is call

BackgroundMediaPlayer.Current

      



And it will run the task, and then your code will get a snooze.

+1


source


I had the same problem too. Background audio recording did not start as soon as it was canceled - either due to 5 minutes of inactivity or due to another application. I have referenced the sample code provided by Microsoft here .

After hours of searching the internet, I have not found a solution. Then, digging further in my code, I found out that when the task is canceled, BackgroundMediaPlayer.Current.CurrentState

it becomes MediaPlayerState.Closed

.



Hence, to restart the task / background replay playback, simply set the source to one BackgroundMediaPlayer.Current

more time. In the code example, this media player object is referenced using a named variable mediaPlayer

inside the PlaylistManager project component.

Although the sample has a piece of code to restart playback after canceling a task, it doesn't work.

0


source







All Articles