WP7 中 BackgroundPlayer 的问题
本文关键字:问题 BackgroundPlayer WP7 | 更新日期: 2023-09-27 18:35:04
我在 wp7 项目中播放流音频时遇到问题。
在解决方案中,我添加新的BackgroundPlayer项目并添加对常规项目的引用。我的音频播放器.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);
}
}
但这不是工作,玩不是开始
怎么了?
我没有
测试它,但经过一些分析,我建议至少以下 2 个更改:
第一个是使用;
AudioPlayer.Playlist.AddRange( .... ); //no ToList() copy needed.
第二个是我认为您的错误来源,将新的音轨添加到播放列表后,未设置单例背景播放器轨道。
尝试添加类似以下内容的内容:
player.Track = Playlist[CurrentTrackPlayed];
我建议进行一些其他更改,例如在控制方法中删除 BackgroundPlayer 播放器参数。
它是一个单例,所以只有 1 个实例。因此,静态属性名称实例。
您可以在整个 AudioPlayer 类中相当简单地使用它,例如您想要的功能更加封装:
public static void AddAndPlayTrack(AudioTrack trackToPlay)
{
BackgroundAudioPlayer.Instance.Track = trackToPlay;
Playlist.Add(trackToPlay);
CurrentTrackPlayed = Playlist.LastIndexOf(trackToPlay);
}