5

I have an audio player that uses BackgroundAudioPlayer together with AudioPlayerAgent. Everything is working fine, except there's one use case I don't know how to handle.

If I play an audio track in my application and then the user switches to another application, the audio track continues to play in the background as it should - but I guess my application has now been swapped to memory and is suspended.

My question is: if the user now starts to play music in another app, I do get the AudioPlayerAgent.UserAction.Stop action as a callback to my AudioAgent. But can I do anything about this now from my app's point of view? I mean, I would like to save the position of the audio playback where the user was in my app, but my app has been suspended, right?

When my app is in the foreground and the audio stops, I do get the BackgroundAudioPlayer.Instance.PlayerState.Stopped event. It's here that I normally save the position of the playback. But if the user switches to another app to play music, I don't get this event.

So I am just wondering how to handle this kind of case. Is there anything I can do to improve the user experience?

Johan Paul
  • 2,203
  • 2
  • 22
  • 38
  • the only way is to save that Position to Isolated Storage in the agent code – onmyway133 Dec 19 '12 at 09:24
  • After writing this question, I was also thinking of the isolated storage. Just wasn't sure if that's accessible from the agent code. Seems it is. Will try. Thanks! – Johan Paul Dec 19 '12 at 09:44
  • @JohanPaul - Make sure you use a [`Mutex`](http://msdn.microsoft.com/en-us/library/system.threading.mutex%28v=vs.95%29.aspx) when accessing the isolated storage, otherwise your foregorund app and background agent could try to access it at the same time. – Richard Szalay Dec 19 '12 at 11:21

1 Answers1

1

The only way is to save that Position to Isolated Storage in the agent code.

See this very useful article of Paul about how to use Background audio agent and the way to communicate between UI and agent side

onmyway133
  • 45,645
  • 31
  • 257
  • 263
  • When I get the PlayState.Stopped event as a result of an external app starting to play, it seems the player.Position property is not available anymore. Any ideas how to get the position of the track before the stopped event happened (or during it...)? – Johan Paul Dec 19 '12 at 13:19