音频播放器代理流 - 停止不删除缓冲区

时间:2012-02-01 15:14:18

标签: c# windows-phone-7 audio-player background-agents

我正在开发一个网络电台应用程序,如果用户按下暂停/停止键,则该流应该停止,当然当他再次按下播放时,该流应该继续。

我遇到的问题是,播放器.Stop()只会暂停音轨。如果再次按下继续,则不会从流中读取前5个秒,而是从缓冲区读取,然后它会在几秒内没有播放声音,然后再次开始从流中读取。

对于网络电台应用来说,这是致命的。我该如何解决?或者我该如何删除缓冲区?

    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();
    }

2 个答案:

答案 0 :(得分:2)

好吧..只是为了让你们知道,这就是我所做的。我不知道是否有更好的方法,但这对我有用:

        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;

答案 1 :(得分:0)

当你停止播放时放弃所有内容,再次播放时又重新启动它怎么样?您使用的功能似乎更适合暂停静态内容。暂停对无线电没有多大意义。