Frage

I'm trying to add a background AudioPlayer to a windows phone 8 application.

I've created the main project and the background audio agent. I've added a reference to the background audio player to the main project, and added the following into the app manifest file.

   <ExtendedTask Name="BackgroundTask">
       <BackgroundServiceAgent Name="myCastsbackgroundaudio" Type="myCastsbackgroundaudio.AudioPlayer" Source="myCastsbackgroundaudio" Specifier="AudioPlayerAgent"/>
    </ExtendedTask>

I've double and triple checked the references and I'm sure they point to the right things.

I'm sharing information between the two applications using IsolatedStorage and the following code

    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;
    }

This would appear to pick up the track name fine, and then attempt to create the Audiotrack for playback. At this point, the application will throw the following error.

    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: 

For the life of me, I can't figure what's causing the error. Nothing jumps out as the root cause, and I've done this a couple of times for other apps with no issue.

Any advice or insight is appreciated.

edit: As requested, I have pasted the OnPLayStateChanged code below

     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();
    }

Also to add some additional info, the error is thrown at the creation of the audio track in GetNextTrack, so this code block

     AudioTrack track = new AudioTrack()
      {
            Title = "Generic Title",
            Source = new Uri("isostore://"+ myTrack, UriKind.Relative)
      };
War es hilfreich?

Lösung

AudioTrack is very sensitive to edits on the track properties. You should use the constructor to set these values, or else use AudioTrack.BeginEdit/EndEdit.

So try:

    var track =
        new AudioTrack(
            new Uri("isostore://"+ myTrack, UriKind.Relative),
            myTrack,
            string.Empty,
            string.Empty,
            null);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top