Timer in AudioPlaybackAgent

I have an internet radio application that uses BackgroundAudioPlayer .

I need a timer in the audio playback agent that updates the track name of the currently playing BAP track pulled from the Internet radio API.

Adding DispatcherTimer to the audio playback agent gives me a threaded exception and uses:

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

Does not work.

I need the code here, because if I put the update code in the application itself, when the user navigates from the application, the updates stop (unlike the behavior of Windows 8).

I cannot use Scheduled Agents since they only run every 30 minutes (IIRC).

Is this possible or impossible to do on Windows Phone?

+6
source share
3 answers

The following is an excerpt from the MSDN documentation for Audio Audio Player:

Sending messages between tasks: There are times when you want to establish a connection between two processes of a background audio application. For example, you might want the background task to notify the foreground task when a new track is launched, and then send the new song name to the foreground task for display on the screen. A simple communication mechanism triggers events in both front and background modes. The SendMessageToForeground and SendMessageToBackground methods each raise events in the corresponding task. Data can be passed as an argument to the event handler in the receiving task. Pass data using the new ValueSet class. This class is a dictionary that contains a string as a key and other types of values ​​as values. You can pass simple value types such as int, string, bool, etc.

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

Hope this helps!

0
source

I found a question that could help you: How to start a background timer in a Windows 8 phone?

when you set a timer that checks every x seconds, if the "name" is different from the last known name, then you can send this information back.

This could be a timer code:

Declare the following:

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

and set this as tick for 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 ()" - this is a function by actually calling your API function.

0
source

Have you considered updating information in the background audio player, as shown 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 this tag in front of the app if necessary?

-1
source

All Articles