InvalidOperation Exception,Background Audio Agent,Windows Phone 8

Dav*_*don 2 background-audio windows-phone-8

我正在尝试将后台AudioPlayer添加到Windows Phone 8应用程序中.

我已经创建了主项目和背景音频代理.我已将背景音频播放器的引用添加到主项目中,并将以下内容添加到应用清单文件中.

   <ExtendedTask Name="BackgroundTask">
       <BackgroundServiceAgent Name="myCastsbackgroundaudio" Type="myCastsbackgroundaudio.AudioPlayer" Source="myCastsbackgroundaudio" Specifier="AudioPlayerAgent"/>
    </ExtendedTask>
Run Code Online (Sandbox Code Playgroud)

我对参考文献进行了两次和三次检查,我确信他们指出了正确的事情.

我正在使用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;
    }
Run Code Online (Sandbox Code Playgroud)

这似乎可以很好地获取曲目名称,然后尝试创建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: 
Run Code Online (Sandbox Code Playgroud)

对于我的生活,我无法弄清楚导致错误的原因.没有任何事情成为根本原因,我已经为其他应用程序做了几次没有问题.

任何建议或见解表示赞赏.

编辑:根据要求,我已粘贴下面的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();
    }
Run Code Online (Sandbox Code Playgroud)

另外,为了添加一些额外的信息,在GetNextTrack中创建音轨时会抛出错误,因此此代码块

     AudioTrack track = new AudioTrack()
      {
            Title = "Generic Title",
            Source = new Uri("isostore://"+ myTrack, UriKind.Relative)
      };
Run Code Online (Sandbox Code Playgroud)

Pau*_*tts 5

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

所以尝试:

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