InvalidOperation Exception,Background Audio Agent,Windows Phone 8

时间:2013-01-28 09:20:18

标签: windows-phone-8 background-audio

我正在尝试将后台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)
      };

1 个答案:

答案 0 :(得分:5)

AudioTrack对曲目属性的编辑非常敏感。您应该使用构造函数来设置这些值,否则使用AudioTrack.BeginEdit/EndEdit

所以试试:

    var track =
        new AudioTrack(
            new Uri("isostore://"+ myTrack, UriKind.Relative),
            myTrack,
            string.Empty,
            string.Empty,
            null);