Windows phone 7 在windows phone后台音频代理中更新歌曲标题?a
我正在为windows phone 7.5制作一个广播应用程序。我想在后台播放收音机。为此,我正在使用windows phone后台音频代理。在代理代码中,我需要指示轨迹和所有与之相关的信息。但我想更新曲目声明中的歌曲标题和艺术家姓名。曲目声明如下所示:Windows phone 7 在windows phone后台音频代理中更新歌曲标题?a,windows-phone-7,Windows Phone 7,我正在为windows phone 7.5制作一个广播应用程序。我想在后台播放收音机。为此,我正在使用windows phone后台音频代理。在代理代码中,我需要指示轨迹和所有与之相关的信息。但我想更新曲目声明中的歌曲标题和艺术家姓名。曲目声明如下所示: new AudioTrack(new Uri("Ring03.wma", UriKind.Relative), "Ringtone 3", "W
new AudioTrack(new Uri("Ring03.wma", UriKind.Relative),
"Ringtone 3",
"Windows Phone",
"Windows Phone Ringtones",
new Uri("shared/media/Ring03.jpg", UriKind.Relative)),
我想在一些变量中存储字符串,然后用“Ringtone 3”替换它们。因此,我不断地更改标题字符串的值。我想每5秒更新一次标题。因此,我希望我的媒体播放器向用户显示曲目信息,这样当用户更改音量时,他会在一个整洁的框中看到姓名和艺术家。我试图做到这一点,但我得到了一个错误:(这听起来像:
错误4字段初始值设定项无法引用非静态字段、方法或属性“MyAudioPlaybackAgent.AudioPlayer.title.get”
请帮助:(
AudioPlayer.cs
using System;
using System.Windows;
using Microsoft.Phone.BackgroundAudio;
namespace AudioPlaybackAgent1
{
public class AudioPlayer : AudioPlayerAgent
{
private static volatile bool _classInitialized;
/// <remarks>
/// AudioPlayer instances can share the same process.
/// Static fields can be used to share state between AudioPlayer instances
/// or to communicate with the Audio Streaming agent.
/// </remarks>
public AudioPlayer()
{
if (!_classInitialized)
{
_classInitialized = true;
// Subscribe to the managed exception handler
Deployment.Current.Dispatcher.BeginInvoke(delegate
{
Application.Current.UnhandledException += AudioPlayer_UnhandledException;
});
}
}
/// Code to execute on Unhandled Exceptions
private void AudioPlayer_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
/// <summary>
/// Called when the playstate changes, except for the Error state (see OnError)
/// </summary>
/// <param name="player">The BackgroundAudioPlayer</param>
/// <param name="track">The track playing at the time the playstate changed</param>
/// <param name="playState">The new playstate of the player</param>
/// <remarks>
/// Play State changes cannot be cancelled. They are raised even if the application
/// caused the state change itself, assuming the application has opted-in to the callback.
///
/// Notable playstate events:
/// (a) TrackEnded: invoked when the player has no current track. The agent can set the next track.
/// (b) TrackReady: an audio track has been set and it is now ready for playack.
///
/// Call NotifyComplete() only once, after the agent request has been completed, including async callbacks.
/// </remarks>
protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
{
switch (playState)
{
case PlayState.TrackEnded:
player.Track = GetPreviousTrack();
break;
case PlayState.TrackReady:
player.Play();
break;
case PlayState.Shutdown:
// TODO: Handle the shutdown state here (e.g. save state)
break;
case PlayState.Unknown:
break;
case PlayState.Stopped:
break;
case PlayState.Paused:
break;
case PlayState.Playing:
break;
case PlayState.BufferingStarted:
break;
case PlayState.BufferingStopped:
break;
case PlayState.Rewinding:
break;
case PlayState.FastForwarding:
break;
}
NotifyComplete();
}
/// <summary>
/// Called when the user requests an action using application/system provided UI
/// </summary>
/// <param name="player">The BackgroundAudioPlayer</param>
/// <param name="track">The track playing at the time of the user action</param>
/// <param name="action">The action the user has requested</param>
/// <param name="param">The data associated with the requested action.
/// In the current version this parameter is only for use with the Seek action,
/// to indicate the requested position of an audio track</param>
/// <remarks>
/// User actions do not automatically make any changes in system state; the agent is responsible
/// for carrying out the user actions if they are supported.
///
/// Call NotifyComplete() only once, after the agent request has been completed, including async callbacks.
/// </remarks>
protected override void OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param)
{
switch (action)
{
case UserAction.Play:
if (player.PlayerState != PlayState.Playing)
{
track.Title = "tracktitle";
player.Play();
}
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:
player.Track = GetNextTrack();
break;
case UserAction.SkipPrevious:
AudioTrack previousTrack = GetPreviousTrack();
if (previousTrack != null)
{
player.Track = previousTrack;
}
break;
}
NotifyComplete();
}
/// <summary>
/// Implements the logic to get the next AudioTrack instance.
/// In a playlist, the source can be from a file, a web request, etc.
/// </summary>
/// <remarks>
/// The AudioTrack URI determines the source, which can be:
/// (a) Isolated-storage file (Relative URI, represents path in the isolated storage)
/// (b) HTTP URL (absolute URI)
/// (c) MediaStreamSource (null)
/// </remarks>
/// <returns>an instance of AudioTrack, or null if the playback is completed</returns>
private AudioTrack GetNextTrack()
{
// TODO: add logic to get the next audio track
AudioTrack track = null;
// specify the track
return track;
}
/// <summary>
/// Implements the logic to get the previous AudioTrack instance.
/// </summary>
/// <remarks>
/// The AudioTrack URI determines the source, which can be:
/// (a) Isolated-storage file (Relative URI, represents path in the isolated storage)
/// (b) HTTP URL (absolute URI)
/// (c) MediaStreamSource (null)
/// </remarks>
/// <returns>an instance of AudioTrack, or null if previous track is not allowed</returns>
private AudioTrack GetPreviousTrack()
{
// TODO: add logic to get the previous audio track
AudioTrack track = null;
// specify the track
return track;
}
/// <summary>
/// Called whenever there is an error with playback, such as an AudioTrack not downloading correctly
/// </summary>
/// <param name="player">The BackgroundAudioPlayer</param>
/// <param name="track">The track that had the error</param>
/// <param name="error">The error that occured</param>
/// <param name="isFatal">If true, playback cannot continue and playback of the track will stop</param>
/// <remarks>
/// This method is not guaranteed to be called in all cases. For example, if the background agent
/// itself has an unhandled exception, it won't get called back to handle its own errors.
/// </remarks>
protected override void OnError(BackgroundAudioPlayer player, AudioTrack track, Exception error, bool isFatal)
{
if (isFatal)
{
Abort();
}
else
{
NotifyComplete();
}
}
/// <summary>
/// Called when the agent request is getting cancelled
/// </summary>
/// <remarks>
/// Once the request is Cancelled, the agent gets 5 seconds to finish its work,
/// by calling NotifyComplete()/Abort().
/// </remarks>
protected override void OnCancel()
{
}
}
}
使用系统;
使用System.Windows;
使用Microsoft.Phone.BackgroundAudio;
命名空间AudioPlaybackAgent1
{
公共级音频播放器:AudioPlayerAgent
{
私有静态易失性bool_分类;
///
///AudioPlayer实例可以共享相同的过程。
///静态字段可用于在AudioPlayer实例之间共享状态
///或与音频流代理进行通信。
///
公共音频播放器()
{
如果(!\u已初始化)
{
_经典化=真实;
//订阅托管异常处理程序
Deployment.Current.Dispatcher.BeginInvoke(委托
{
Application.Current.UnhandledException+=音频播放器\u UnhandledException;
});
}
}
///要在未处理的异常上执行的代码
私有void AudioPlayer_未处理的异常(对象发送方、应用程序未处理的异常事件)
{
if(System.Diagnostics.Debugger.IsAttached)
{
//发生未处理的异常;请中断调试器
System.Diagnostics.Debugger.Break();
}
}
///
///播放状态更改时调用,错误状态除外(请参阅OneError)
///
///背景音频播放器
///播放状态更改时播放的曲目
///玩家的新游戏状态
///
///无法取消播放状态更改。即使应用程序
///导致状态本身发生更改,假设应用程序已选择回调。
///
///值得注意的playstate事件:
///(a)TrackEnded:当播放器没有当前曲目时调用。代理可以设置下一个曲目。
///(b)TrackReady:已设置音频曲目,现在可以播放。
///
///在代理请求完成(包括异步回调)后,仅调用NotifyComplete()一次。
///
受保护的覆盖无效OnPlayStateChanged(BackgroundAudioPlayer播放器、AudioTrack曲目、PlayState播放状态)
{
开关(播放状态)
{
案例PlayState.TrackEnded:
player.Track=GetPreviousTrack();
打破
case PlayState.TrackReady:
player.Play();
打破
案例播放状态。关机:
//TODO:在此处处理关机状态(例如保存状态)
打破
案例播放状态。未知:
打破
案例播放状态。已停止:
打破
案例播放状态。已暂停:
打破
案例播放状态。播放:
打破
case PlayState.BufferingStarted:
打破
case PlayState.BufferingStopped:
打破
案例播放状态。倒带:
打破
case PlayState.fastfrowarding:
打破
}
NotifyComplete();
}
///
///当用户使用应用程序/系统提供的UI请求操作时调用
///
///背景音频播放器
///用户操作时播放的曲目
///用户请求的操作
///与请求的操作关联的数据。
///在当前版本中,此参数仅用于查找操作,
///指示音频曲目的请求位置
///
///用户操作不会自动对系统状态进行任何更改;由代理负责
///用于执行用户操作(如果支持)。
///
///在代理请求完成(包括异步回调)后,仅调用NotifyComplete()一次。
///
受保护的覆盖无效OnUserAction(BackgroundAudioPlayer播放器、AudioTrack曲目、UserAction动作、对象参数)
{
开关(动作)
{
case UserAction.Play:
如果(player.PlayerState!=PlayState.Playing)
{
track.Title=“tracktitle”;
player.Play();
}
打破
案例用户操作。停止:
player.Stop();
打破
案例用户操作。暂停:
player.Pause();
打破
case UserAction.FastForward:
player.fastfroward();
打破
案例用户操作。倒带:
player.Rewind();
打破
案例用户操作
using Microsoft.Phone.BackgroundAudio;
using System.Windows.Media;
namespace AudioStreamAgent1
{
/// <summary>
/// A background agent that performs per-track streaming for playback
/// </summary>
public class AudioTrackStreamer : AudioStreamingAgent
{
/// <summary>
/// Called when a new track requires audio decoding
/// (typically because it is about to start playing)
/// </summary>
/// <param name="track">
/// The track that needs audio streaming
/// </param>
/// <param name="streamer">
/// The AudioStreamer object to which a MediaStreamSource should be
/// attached to commence playback
/// </param>
/// <remarks>
/// To invoke this method for a track set the Source parameter of the AudioTrack to null
/// before setting into the Track property of the BackgroundAudioPlayer instance
/// property set to true;
/// otherwise it is assumed that the system will perform all streaming
/// and decoding
/// </remarks>
protected override void OnBeginStreaming(AudioTrack track, AudioStreamer streamer)
{
//TODO: Set the SetSource property of streamer to a MSS source
NotifyComplete();
}
/// <summary>
/// Called when the agent request is getting cancelled
/// The call to base.OnCancel() is necessary to release the background streaming resources
/// </summary>
protected override void OnCancel()
{
base.OnCancel();
}
}
}
var player = BackgroundAudioPlayer.Instance;
player.Track = "new track name";