2

This is another background audio resources are no longer available error in Windows Phone 7, but a different scenario. I have 2 apps, which both use BackgroundAudioPlayer to play background music.

When app A is playing background music, I press Home button to choose app B, play some music in app B, then return to app A.

There whenever app A use BackgroundAudioPlayer.Instance, it will throw InvalidOperationException with message "background audio resources are no longer available".

The only thing I can do is try/catch, which is of no use other than preventing the app to crash. And, I cannot call BackgroundAudioPlayer.Instance.Close() because it is no longer available.

How to deal with this ?

Community
  • 1
  • 1
onmyway133
  • 45,645
  • 31
  • 257
  • 263

1 Answers1

0

In this scenario you cannot rely on BackgroundAudioPlayer.Instance methods working - and you need to handle the InvalidOperationException to know that this has scenario occurred.

At this point you can restart your background audio agent again using BackgroundAudioPlayer.Instance.Play().

Paul Annetts
  • 9,554
  • 1
  • 27
  • 43
  • the Instance is no longer available? So how can I call Play() ? And, should I call NotifyComplete in AudioPlayer_UnhandledException (agent side) ? – onmyway133 Dec 18 '12 at 03:50
  • The *resources* are no longer available. `Play()` will recreate them so you can call the normal methods after that point. – Paul Annetts Dec 18 '12 at 10:08
  • thanks. Play() is OK. But when I call SkipNext, SkipPrevious, .. these user actions seems to be queued until Play() is called. There the agent process SkipNext, SkipPrevious and Play respectively. Do you know how to distinct if that music is played by our app or another app ? – onmyway133 Dec 18 '12 at 11:13
  • 1
    Did you try calling `BackgroundAudioPlayer.Instance.PlayerState`? I suspect if it throws you aren't in control of audio any more. – Paul Annetts Dec 18 '12 at 11:59