WP7 background audio resources are no longer available - windows-phone-7

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 ?

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.

Related

Recording Audio Stream in Background in Windows phone 7/8

I m developing an app which can record audio stream.
I want to know that is it possible to record that audio in background, i,e when the application is in deactivated mode?
I have already tried to do that in many different ways,but its not helping out.
Please suggest me some way to do such...
Thank you.
As mentioned in the comments, it's not possible to record in the background.
Your app must be running in the foreground for it to be able to record audio using the microphone. For an overview of all the audio recording options, please refer to this tutorial on the Nokia Developer Wiki.
Also, an app will not run when the phone is locked - it is suspended. So if your app was recording, it would stop when the phone is locked. You can change this behaviour with IdleDetectionMode.Disabled which means your app keeps running even though the phone is locked.
Haven't implemented it before, but classifying it as a VOIP app could be the way to go:
http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207046(v=vs.105).aspx

How to handle error when two apps are using BackgroundAudioPlayer

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 ?
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().

How to play mp3 file in loop in WP7

I'm implementing a game for WP7, and I need that a mp3 file plays in background, while I navigate through some screens. How can I do this? Thanks in advance.
Use background Audio agent for this purpose to play , even when user navigate between pages.
Also you can achieve the continuous playback by 2 ways
1. set the repeat option to play continuously
or
2. start play back again upon receiving the stop message, this can be achieved by putting code for starting playback again in Application Background player state handler upon receiving "PlayState.Stopped" message.
Please refer this link "How to: Play Background Audio for Windows Phone" for more details.
Hm.. what about global MediaPLayer instance?
See this link

how to code to stop/pause zune music in windowsphone7

Does anyone knows, how to code in c# to stop/pause the zune music in windowsphone7. I used background music in my application, if i launch my application, zune music stops. how to handle this? i use windowsphone7 version, so cant able to use BackgroundAudioPlayer instance.
You shouldn't stop the users background music if it's already playing and you're not going to play music yourself.
If you wish to play music in your app then (after confirming with the user that it's ok to stop the currently plaing music) just start playing your music and this will then cause the music that was playing to be stopped.
There is no way to automatically resume the musc that was playing prevviously when you stop playing your music.

Accessing MediaPlayer in Background Agent in Windows Phone 7

I am trying to capture the current playing track in the Zune music player, in my Background Agent app. Looking at the Unsupported APIs for Background Agents page on MSDN, all of XNA is not supported - which means I can't use XNA's Media.MediaPlayer. Is there any workaround or solution for this?
Nope.
There is currently no way to access anything other than the tracks which you are playing in a BackgroundAudioPlayer agent.
Actually, there is no way to access anything other than THE track (not tracks) that the background audio player is playing. There is no concept of a playlist exposed by the BAP singleton, which apparently is the source for much frustration with this component. You have to resort to IsolatedStorage or some other hack to feed the BAP a playlist.

Resources