我在我的 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);
}
}
但它不起作用,播放没有开始
怎么了?