1

I need to store background audio play position in case another application gets started and takes over background audio player facility.

I have used the following in my audio player agent

protected override void OnPlayStateChanged(BackgroundAudioPlayer _player, AudioTrack _track, PlayState _playState)
{
    System.Diagnostics.Debug.WriteLine(_playState.ToString());

    double _pos = -1;

    try
    {
        _pos = _player.Position.TotalSeconds;
    }
    catch (Exception _ex)
    {
        System.Diagnostics.Debug.WriteLine(_ex.Message);
    }

    switch (_playState)
    {
    // handling of different state changes
    }

    myStorePositionRoutine(_pos)

    NotifyComplete();

}

Other state changes coming from e.g. buffering or play / pause etc events seem to work fine and I am able to get the latest position recorded for possible later use.

However, if and when (while running my app in debugger) I start another application which takes over the background audio player facility in the phone, the first (and last) event in debugger I can see is play state

"Stopped"

and the exception message

"The background audio resources are no longer available."

Is there something I could do to force the background audio player agent make regular position saves in anticipation of a "non graceful" shut down - or is there some other way / event to use get and store the position preceding that stopped status and unavailability of background audio resources?

Romasz
  • 29,662
  • 13
  • 79
  • 154
juhariis
  • 568
  • 8
  • 15

1 Answers1

0

When you invoke other Player, yours is being Shutdown (as there can be only one instance of BAP) and its resources won't be available - that's why you lose your _pos. To save it between instances you will have to use a file or IsolatedStorageSettings. You can save every time the User stops playing or only when Shutdown is invoked. The sample code can look like this:

private static double _pos = 0;

protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
{
    switch (playState)
    {
       case PlayState.Shutdown:
            IsolatedStorageSettings IS = IsolatedStorageSettings.ApplicationSettings;
            if (IS.Contains("position")) IS["position"] = _pos;
            else IS.Add("position", _pos);
            IS.Save(); // remember to save
            // rest of the code


// And somewhere where you want to bring your position back (for example when your BAP starts):
// to retrive the position:
IsolatedStorageSettings IS = IsolatedStorageSettings.ApplicationSettings;
if (IS.Contains("position")) _pos = (double)IS["position"];
else _pos = 0;
// rest of the code

And make your _pos static and outside OnPlayStateChanged, then it will be also available for OnUserAction.

Romasz
  • 29,662
  • 13
  • 79
  • 154
  • Thanks but I have no problem in getting and storing position when things happen in orderly fashion. But with another application "grabbing" control over background audio player facility in the phone my own agent moves from a perfectly happy state "Playing" to state "Stopped" (where I am not able to get position anymore) and finally to "Shutdown" (where I am not able to get position anymore). Based on this thread http://stackoverflow.com/questions/9702935/audioplayeragent-timer-and-webservice I may have to accept this limitation. – juhariis Jun 11 '14 at 17:55
  • I see, so you say that when you invoke other player, yours first get Stop and it's impossible to get its `Position`? If that is the case, I'm not sure if you will be able to do much, as it doesn't depend on your code. – Romasz Jun 11 '14 at 20:19
  • @juhariis Have you tried to put the SavePositionCode in Stop/Shutdown and run it as Release without debugger? While running a debugger, when it hit a breakpoint it might be too late to read the Position - the BAP may already be gone (you have limited time). – Romasz Jun 12 '14 at 05:30
  • that was a good idea. Unfortunately the test I made using your suggestion was not succesful. Then again, I think this limitation is quite acceptable for the users. Position gets saved when play state changes either due to buffering, track ending or the user interacting with the audio player (either via application control or by using universal volume control). Starting another app is (hopefully) a small minority of use cases. – juhariis Jun 12 '14 at 17:00