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
.