WP7 how to start audio agent process - windows-phone-7

I'm using BackgroundAudioPlayer agent for my Windows Phone 7 application. I know that the AudioPlayer class lives in another process (let's call it audio agent process).
I heard that "In theory the process for an Audio Player agent could get started and stopped at any time".
Sometimes, when I press play on my UI, the agent process seems to NOT start, and the agent code doesnot receive any OnUserAction event. And this cause my application to behave wrongly.
I want the agent process to be ready when I want to play music. How to fix this ?

Maybe you need to start playing audio inside dispatcher? Something like next:
Dispatcher.BeginInvoke( () => { StartAudioOrSoOn(); });
Try to check examples provided by Microsoft for Windows Phone developers:
Windows Phone Samples: learn through code

Related

Windows Phone XNA Games Hangs on Resume when Guide is Visible on WP7.x

My Windows Phone XNA game calls Guide.BeginShowKeyboardInput to get the user's name. While the Guide's input screen is shown, if the user presses the Home or Search (hardware) buttons, the game is deactivated as usual - but if the user then presses the Back (hardware) button, the game should resume but it doesn't. Instead it shows the "Resuming..." message until Windows Phone gives up trying to resume the game and kills the process.
Please note:
My app is failing Microsoft certification because of this problem - I really need to fix it!
This only happens in my game when the Guide input screen is shown - when it's not shown, the game
resumes properly after being deactivated.
This problem only occurs when the game is run on WP7.x (verified problem on WP7.5 and
WP7.8) - although the game seems to resume properly on WP8 devices.
I've created a blank XNA game project, called Guide.BeginShowKeyboardInput and tested it using WP7.x devices, and this problem-behavior doesn't occur.
I've tested the game without enabling Music & my Trial License manager (they use timers) and it still does this.
Exact same behavior occurs in the emulator (resume-failure on WP7.x emulators, works fine on WP8
emulator)
I used threads to asynchronously download content during the loading screen - but they already completed and exited.
I don't believe other threads are running, although this seems like
it could be a thread-blocking issue.
Any other ideas on what could be blocking the game from resuming when the Guide.IsVisible or how to debug/resolve this problem?
The solution to this issue was simply to NOT use SuppressDraw when dealing with any operation that passes control outside of the app, e.g. Launchers, Choosers and the Guide.
Have you tried calling "Guide.EndShowKeyboardInput" in either the Deactivating or closing events?
So that the guide is closed before the app suspends, you do get 10 seconds to suspend so this might be worth looking into.
If you want the guide to re-show on resume, then in your state saving code just have a flag and open the guide if it's set.

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

WP7 background audio resources are no longer available

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.

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.

Resources