我正在尝试将背景 AudioPlayer 添加到 Windows Phone 8 应用程序。
我已经创建了主项目和后台音频代理。我在主项目中添加了对背景音频播放器的引用,并将以下内容添加到应用清单文件中。
<ExtendedTask Name="BackgroundTask">
<BackgroundServiceAgent Name="myCastsbackgroundaudio" Type="myCastsbackgroundaudio.AudioPlayer" Source="myCastsbackgroundaudio" Specifier="AudioPlayerAgent"/>
</ExtendedTask>
我已经两次和三次检查了参考文献,我确信它们指向正确的东西。
我正在使用 IsolatedStorage 和以下代码在两个应用程序之间共享信息
private AudioTrack GetNextTrack()
{
string myTrack = settingsHelper.Read("track", string.Empty);
AudioTrack track = new AudioTrack()
{
Title = "Generic Title",
Source = new Uri("isostore://"+ myTrack, UriKind.Relative)
};
return track;
}
这似乎可以很好地获取曲目名称,然后尝试创建 Audiotrack 以进行播放。此时,应用程序将抛出以下错误。
System.InvalidOperationException was unhandled
_HResult=-2146233079
_message=Operation is not valid due to the current state of the object.
HResult=-2146233079
Message=Operation is not valid due to the current state of the object.
Source=Microsoft.Phone
StackTrace:
at Microsoft.Phone.BackgroundAudio.AudioTrack.set_Title(String value)
at myCastsbackgroundaudio.AudioPlayer.GetNextTrack()
at myCastsbackgroundaudio.AudioPlayer.OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
at Microsoft.Phone.BackgroundAudio.AudioPlayerAgent.CallOnPlayStateChanged(ParameterPropertyBag parameters)
at Microsoft.Phone.BackgroundAudio.AudioPlayerAgent.Invoke(Uri uri, ParameterPropertyBag parameters)
at Microsoft.Phone.BackgroundAgentDispatcher.AgentRequest.Invoke()
at Microsoft.Phone.BackgroundAgentDispatcher.InvocationThread()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
对于我的一生,我无法弄清楚是什么导致了错误。没有什么是根本原因跳出来的,我已经为其他应用程序做了几次,没有任何问题。
任何建议或见解表示赞赏。
编辑:根据要求,我在下面粘贴了 OnPLayStateChanged 代码
protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
{
switch (playState)
{
case PlayState.TrackEnded:
player.Track = GetNextTrack();
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();
}
还要添加一些附加信息,在 GetNextTrack 中创建音轨时会引发错误,因此此代码块
AudioTrack track = new AudioTrack()
{
Title = "Generic Title",
Source = new Uri("isostore://"+ myTrack, UriKind.Relative)
};