Search code examples
c#windows-phone-7audio-playerbackground-agents

AudioPlayerAgent Streaming - Stop doesnt delete the buffer


I'm currently developing a web radio app and if the user presses the pause/stop key the stream should stop and of course when he presses play again the stream should continue.

The problem I have is, that player.Stop() only pauses the track. If you press continue again, the first 5 secounds are not read from the stream but from a buffer, then it playes no sound for a few secounds and then begins to read from the stream again.

This is fatal for a web radio app. How can I fix it? Or how can I delete the buffer?

    protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
    {
        switch (playState)
        {
            case PlayState.TrackReady:
                player.Play();
                break;
            case PlayState.Stopped:
                player.Stop();
                break;
            case PlayState.Paused:
                player.Stop();
                break; 
        }

        NotifyComplete();
    }

Solution

  • Allright.. just to let you guys know, this is what I did. I don't know if there's any better way but this works for me:

            protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
        {
            switch (playState)
            {
                case PlayState.Stopped:
                    track.BeginEdit();
                    track.Tag = track.Source.OriginalString;
                    track.Source = new Uri("http://127.0.0.1", UriKind.Absolute);
                    track.EndEdit();
    
                    player.Track = track;
                    break;
    
    
            protected override void OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param)
        {
            switch (action)
            {
                case UserAction.Play:
                    if (player.PlayerState != PlayState.Playing)
                    {
                        try
                        {
                            player.Play();
                        }
                        catch(Exception)
                        {
                            track.BeginEdit();
                            track.Source = new Uri(player.Track.Tag, UriKind.Absolute);
                            track.EndEdit();
    
                            player.Track = track;
                            player.Play();
                        }
                    }
                    break;
                case UserAction.Stop:
                case UserAction.Pause:
                    if (player.Track.Source.OriginalString != "http://127.0.0.1/")
                    {
                        player.Stop();
                    }
                    break;