Question

In my app, I want to be know when play state changes. But I don't know how to subscribe to the event and get the current state. How can I do that? thanks.

I see an statement in MSDN, but couldn't understand what it means and how to implement it:

In Windows Phone 8, you can check the PlayStateChangedEventArgs to determine both the CurrentPlayState and the IntermediatePlayState that occurred before the audio player entered the current play state.

Details:

in the main page I do this:

public MainPage()
{
    BackgroundAudioPlayer.Instance.PlayStateChanged += new EventHandler(Instance_PlayStateChanged);
}

then

private void Instance_PlayStateChanged(object sender, EventArgs e)
{
    var playerState = BackgroundAudioPlayer.Instance.PlayerState;
}

But I feel this is not the correct way to use event and eventargs. it also doesn't give me the correct latest value.

Was it helpful?

Solution

The PlayerStateChanged event is definitely the right way to determine changes, but it won't fire when you subscribe to it so you won't get the current state. Try something like this instead:

BackgroundAudioPlayer audioPlayer = BackgroundAudioPlayer.Instance;

public MainPage()
{
    audioPlayer += OnPlayStateChanged;
    OnPlayStateChanged(audioPlayer.PlayerState);
}

private OnPlayStateChanged(object sender, EventArgs e)
{
    OnPlayStateChanged(audioPlayer.PlayerState);
}

private OnPlayStateChanged(PlayState state)
{
    // Process state here
}

Having said that, there are two major things worth pointing out.

Firstly, BackgroundAudioPlayer is an extremely volatile API. It will commonly throw exceptions if not in the correct internal state. Feel free to use the extension methods I developed for Podcaster: https://gist.github.com/richardszalay/8552812

Secondly, PlayerStateChanged is not fired when the playback position changes. For that, I'd recommend using a DispatcherTimer and updating your display via my TryGetPosition method (but only when GetTrackOrDefault() returns non-null). I'd also recommend using a sub-second timer (200-300ms) to keep the "ticking" correct. When the PlayerState changes to FastForwarding or Rewinding, update the timer to 20-30ms, and restore it when it returns to Playing.

OTHER TIPS

Use this solution as well as link you would get solution:-

enter link description here

 void Instance_PlayStateChanged(object sender, EventArgs e)
    {
        switch (BackgroundAudioPlayer.Instance.PlayerState)
        {
          case PlayState.Playing:
            playButton.Content = "pause";
            break;

          case PlayState.Paused:
          case PlayState.Stopped:
            playButton.Content = "play";
            break;
        }

        if (null != BackgroundAudioPlayer.Instance.Track)
        {
          txtCurrentTrack.Text = BackgroundAudioPlayer.Instance.Track.Title +
                                 " by " +
                                 BackgroundAudioPlayer.Instance.Track.Artist;
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top