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();
}
}
}
私の一般的なプロジェクトでは、最初に新しいオーディオ トラックを Playlist オブジェクトに追加します。
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);
}
}
しかし、それは仕事ではありません、遊びは始まりません
どうしたの?