Timer in AudioPlaybackAgent

185 views Asked by At

I have an Internet radio app that uses BackgroundAudioPlayer.

I need a timer in the Audio Playback Agent that will update the track title of the currently playing track of the BAP that is pulled from the Internet radio station's API.

Adding a DispatcherTimer into the Audio Playback Agent gives me a cross-thread exception, and using:

Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                // Code
            });

Didn't work.

I need the code in here because if I put the update code in the app itself, when the user navigates away from the app the updates stop (much unlike Windows 8's behavior).

I can't use Scheduled Agents since they only run once every 30 minutes (IIRC).

Is this possible or can this not be done on Windows Phone?

3

There are 3 answers

0
juhariis On

Have you considered updating the information in the background audio player agent as below in the track tag.

string newTag = "whatever you need to show";
AudioTrack track = BackgroundAudioPlayer.Instance.Track;
track.BeginEdit();
track.Tag = newTag;
track.EndEdit();

and then reading that tag in the front end by your application when needed?

1
CodeNoob On

Below is an excerpt from the MSDN documentation for Background Audio Player:

Sending messages between tasks: There are times when you will want to communicate between the two processes of a background audio app. For example, you might want the background task to notify the foreground task when a new track starts playing, and then send the new song title to the foreground task to display on the screen. A simple communication mechanism raises events in both the foreground and background processes. The SendMessageToForeground and SendMessageToBackground methods each invoke events in the corresponding task. Data can be passed as an argument to the event handler in the receiving task. Pass data using a new class called ValueSet. This class is a dictionary that contains a string as a key and other value types as values. You can pass simple value types such as int, string, bool, and so on.

https://msdn.microsoft.com/en-US/library/windows/apps/xaml/dn642090

Hope this helps!

0
Daniel Alexander Karr On

I found a question which could help you : How to run a timer on background in windows phone 8?

when you set a timer which is checking every x seconds if the "title" differs from last known title then you could send this info back to it.

This could be the Code for the Timer:

Declare these:

string _newValue = string.Empty;
string _currentValue = string.Empty;
AudioTrack _tempTrack = null;

and set this as Tick for the Timer

if (this.BackgroundAudioPlayer != null)
{
   if (this.BackgroundAudioPlayer.Instance != null)
   {
       if (this.BackgroundAudioPlayer.Instance.Track != null)
       {
           this._newValue= yourAPI.GetTitleOfTrack();

           try
           {
               /* First try to get the current Track as own Var */
               this._tempTrack = this.BackgroundAudioPlayer.Instance.Track;
               if (this._tempTrack != null)
               {
                  /* Then Read the .Tag Value from it, save to _currentValue */
                  if (this._tempTrack.Tag != null) 
                  { this._currentValue = this._tempTrack.Tag.ToString(); }
                  else
                  { this._currentValue = string.Empty; }

                  /* Compare */
                  if (this._currentValue != this._newValue)
                  {
                     /* Edit the Track Tag from your original BAP */
                     this.BackgroundAudioPlayer.Instance.Track.Tag = this._newValue;
                  }
               }
           }
           catch(Exception ex)
           {
               /* if something Crashes you can save the exception error for protocol */
           }
       }
   }
}

Remember: Change "yourAPI.GetTitleOfTrack()"-Function from this with real Function Call of your API.