0

我在我的 wp7 项目中播放流音频时遇到问题。

在解决方案中,我添加了新的 BackgroundPlayer 项目并添加了对一般项目的引用。我的 AudioPlayer.cs 看起来接下来:

public class AudioPlayer : AudioPlayerAgent
{
    public static List<AudioTrack> Playlist = new List<AudioTrack>();
    public static int CurrentTrackPlayed;
    private void PlayTrack(BackgroundAudioPlayer player)
    {
        player.Track = Playlist[CurrentTrackPlayed];
        player.Play();
    }
    private void PlayNext(BackgroundAudioPlayer player)
    {
        if (++CurrentTrackPlayed >= Playlist.Count)
            CurrentTrackPlayed = 0;
        PlayTrack(player);
    }
    private void PlayBack(BackgroundAudioPlayer player)
    {
        if (--CurrentTrackPlayed < 0)
            CurrentTrackPlayed = Playlist.Count - 1;
        PlayTrack(player);
    }
    private static volatile bool _classInitialized;

    public AudioPlayer()
    {
        if (_classInitialized) return;
        _classInitialized = true;
        Deployment.Current.Dispatcher.BeginInvoke(delegate
                                                      {
                                                          Application.Current.UnhandledException += AudioPlayerUnhandledException;
                                                      });
    }
    private static void AudioPlayerUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    {
        if (System.Diagnostics.Debugger.IsAttached)
        {
            System.Diagnostics.Debugger.Break();
        }
    }
    protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
    {
        switch (playState)
        {
            case PlayState.TrackEnded:
                PlayNext(player);
                break;
            case PlayState.TrackReady:
                player.Play();
                break;
            case PlayState.Shutdown:
                // TODO: обработайте здесь состояние отключения (например, сохраните состояние)
                break;
        }

        NotifyComplete();
    }
    protected override void OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param)
    {
        MessageBox.Show(action.ToString());
        switch (action)
        {
            case UserAction.Play:
                PlayTrack(player);
                break;
            case UserAction.Stop:
                player.Stop();
                break;
            case UserAction.Pause:
                player.Pause();
                break;
            case UserAction.FastForward:
                player.FastForward();
                break;
            case UserAction.Rewind:
                player.Rewind();
                break;
            case UserAction.Seek:
                player.Position = (TimeSpan) param;
                break;
            case UserAction.SkipNext:
                PlayNext(player);
                break;
            case UserAction.SkipPrevious:
                PlayBack(player);
                break;
        }

        NotifyComplete();
    }
    protected override void OnError(BackgroundAudioPlayer player, AudioTrack track, Exception error, bool isFatal)
    {
        if (isFatal)
        {
            Abort();
        }
        else
        {
            NotifyComplete();
        }

    }
}

在我的一般项目中,首先我将新的音轨添加到播放列表对象:

AudioPlayer.Playlist = audios.Select(a => new AudioTrack(new Uri(a.Url, UriKind.Absolute), a.Title, a.Artist, null, null)).ToList();
                AudioPlayer.CurrentTrackPlayed = 0;

接下来我尝试使用以下代码播放歌曲:

private void PlaySound(Audio a, Shape r)
    {
        try
        {
            if (_lastSelector != null)
                _lastSelector.Fill = Application.Current.Resources["PhoneInverseBackgroundBrush"] as SolidColorBrush;
            if (searchBox.Visibility == Visibility.Visible)
                Store.CurrentAudioIndex = _searched.IndexOf(a);
            else if (_isAudios)
                Store.CurrentAudioIndex = _audios.IndexOf(a);
            else if (_isRecommendations)
                Store.CurrentAudioIndex = _recommendations.IndexOf(a);
            BackgroundAudioPlayer.Instance.Play();
            if (r != null)
            {
                r.Fill = VKMobile.Resources.AccentBrush;
                _lastSelector = r;
            }
        }catch(Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }

但它不起作用,播放没有开始

怎么了?

4

1 回答 1

0

我没有对其进行测试,但经过一些分析后,我建议至少进行以下两项更改:

第一个是使用;

 AudioPlayer.Playlist.AddRange( .... ); //no ToList() copy needed.

第二个是我认为错误的根源,在将新的 AudioTracks 添加到播放列表后,未设置 Singleton Backgroundplayer Track。

尝试添加这样的东西:

player.Track = Playlist[CurrentTrackPlayed];

我建议进行一些其他更改,例如在您的控制方法中删除 BackgroundPlayer 播放器参数。

它是一个单例,所以只有一个实例。因此静态属性名称为 Instance。

您可以在整个 AudioPlayer 类中相当简单地使用它,并将您想要的功能封装得更紧密:

public static void AddAndPlayTrack(AudioTrack trackToPlay)
    {
        BackgroundAudioPlayer.Instance.Track = trackToPlay;
        Playlist.Add(trackToPlay);
        CurrentTrackPlayed = Playlist.LastIndexOf(trackToPlay);
    }
于 2012-05-23T21:27:24.463 回答