Background Audio Track Ready not working properly Windows Phone 8

592 views Asked by At

I've found a strange problem and I'll try to explain it as short as possible.

ASSUMTIONS:

According to my knowledge as you set a new AudioTrack to AudioPlayerAgent, two task are queued: Stop(), and TrackReady();. Seems logic and in most cases works perfect. But as I was working with my program, I spotted that sometimes it's not so good:

PROBLEM:

So I decided to write a very simple example - you can get it here. There is only one buton that calls BackgroundAudioPlayer.Instance.Play();. (You have to add only music.mp3 file in that example - for example 5-6 Mb mp3 file). In Audio Agent I have:

case UserAction.Play:
if (player.PlayerState != PlayState.Playing)
      player.Track = myMusic.ReturnTrack();
break;

I run program in Debug mode on Device (WP8) and push my play button. And then I see in most cases that OnPlayStateChanged in Audio Agent is not fired, and looking at processes, I see I have Headless 'Zombie'. Seems to be a deadlock. (BTW: in this simple example I'm not using any Mutexes or other concurrency techniques) Strange (or maybe not) is also that Stop() and TrackReady are waiting in a queue - if you fire any other method that calls NotifyComplete(); you will see that queue gets unblocked.
I've tried Realese version without debbuging and it works better BUT - sometimes happens the same.

If I only add:

case UserAction.Play:
if (player.PlayerState != PlayState.Playing)
{
      player.Track = myMusic.ReturnTrack();
      Thread.Sleep(100);
}
break;

everything works a lot better.

CONCLUSION:

It seems to me that loading new track works asynchronously and NotifyComplete() is called before track is loaded - what creates this 'headless zombie' process. The worst thing is that you have limited time for operation waiting in queue, after it runs out, your Agent is being killed.
In my opinion it shouldn't work like that, as setting new track is one of the major functions.

Does anybody know something about this problem? Is it a bug or misbehaviour of OS or my lack of knowledge?

1

There are 1 answers

6
Paul Annetts On

You've wired this up incorrectly - it's worth trying to check the default implementation of a background audio agent and compare with that. The main issue would appear that you are trying to change track in OnUserAction.Play, which the APIs don't expect...

You need to specify your track in:

    protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
    {
        switch (playState)
        {
            case PlayState.TrackEnded:
                player.Track = myMusic.ReturnTrack();
                break;

And leave the OnUserAction as:

    protected override void OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param)
    {
        switch (action)
        {
            case UserAction.Play:
                if (player.PlayerState != PlayState.Playing)
                {
                    player.Play();
                }
                break;