Timer in AudioPlaybackAgent
Asked Answered
T

3

6

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?

Torrid answered 8/12, 2013 at 11:22 Comment(0)
A
0

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!

Alizaalizarin answered 5/1, 2016 at 9:32 Comment(1)
This will not work for the problem listed above. The user navigates away from the application so the foreground is not available to receive the message. I (and the OP) am looking to periodically (let's say every 6 seconds) perform some task regardless of the UI is open while the background audio is playing. The only way I now how to do this so far is to implement an Audio Streaming Agent. I want to know if there is an easy way. Thank you for your replay though.Spermatic
S
0

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.

Seleta answered 8/1, 2016 at 13:2 Comment(0)
A
-1

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?

Albertinaalbertine answered 11/6, 2014 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.