1

I'm using BackgroundAudioPlayer for my Windows Phone 7 music & video application. After I play some music, I play video using MediaPlayerLauncher, then press Back to return to my app. There whenever I use BackgroundAudioPlayer.Instance. I receive error "The background audio resources are no longer available".

Someone on MSDN suggests using try/catch, but this is not a good idea, and can slow down the app.

Other suggests call BackgroundAudioPlayer.Instance.Close() before launch MediaPlayer. However, when I play music, the agent load .dll again, which takes very much time.

How to fix this ?

Paul Annetts
  • 9,554
  • 1
  • 27
  • 43
onmyway133
  • 45,645
  • 31
  • 257
  • 263

1 Answers1

2

If you play a video after your audio the OS will definitely "terminate" your Background Audio Player. From your question it seems this is reproducible 100% of the time which would confirm this. Your only option is to restart the background audio player again after you have called BackgroundAudioPlayer.Instance.Close(), and then played your video. Which as you said will require reloading your player DLL when you start the BAP.

Update following up from comments

If you aren't implementing a streaming audio agent but only an AudioPlayer agent there isn't a process for you to kill anyway. The OS spins up a process as and when it needs to get you to process an action (e.g.: user action, track ended, shutdown).

BackgroundAudioPlayer.Instance.Close() just makes sure that the OS releases those resources cleanly in a scenario such as the OP has.

To restart background audio, just call BackgroundAudioPlayer.Instance.Play() again.

Paul Annetts
  • 9,554
  • 1
  • 27
  • 43
  • in my app, it works when I call Instance.Close() before launching MediaPlayer. When I play music, I see it loads .dlls. But as you mentioned "restart agent using Instance.Close()", so the agent should load .dlls right after I call Close() ? – onmyway133 Dec 01 '12 at 15:22
  • Good point - Close() won't reload the DLLs. I've updated the answer. – Paul Annetts Dec 01 '12 at 22:14
  • thanks, but what does Close() do? You said that the OS will terminate agent when I play video, so there's no need to call Close() ? – onmyway133 Dec 02 '12 at 15:39
  • 1
    Close() will close the BAP more cleanly so you don't get exceptions thrown if you try accessing the old terminated BAP instance – Paul Annetts Dec 02 '12 at 17:22
  • Close() terminates the BAP instance? or terminate the process it lives in? I see it reloads .dlls, so that it's the PROCESS that was killed ? – onmyway133 Dec 04 '12 at 02:56
  • And how can I restart BackgroundAudioPlayer after Close called? Please see this topic http://stackoverflow.com/questions/13769275/how-to-reuse-backgroundaudioplayer-after-close-method-called – Nazar Grynko Dec 07 '12 at 21:00
  • @Yama moto: If you aren't implementing a Streaming audio agent but only an AudioPlayer agent there isn't a process for you to kill anyway. The OS spins up a process as and when it needs. I'll update the answer with this. – Paul Annetts Dec 10 '12 at 12:08