2

I have an app which has two background tasks: a background audio task and a Bluetooth task. What I want to do is modify the state of the audio playback from within the code in the Bluetooth task without having to go through the foreground app. I tried putting this in the Bluetooth task hoping it'd work:

if (BackgroundMediaPlayer.Current.CurrentState == MediaPlayerState.Playing)
   BackgroundMediaPlayer.Current.Pause();

But it turns out it doesn't work. Even though the background audio is playing, when I access its state from the Bluetooth task its value is equal to MediaPlayerState.Closed(). Anyone have any idea how I can go about doing this? I thought this'd work because BackgroundMediaPlayer is a global object which only has one instance in the entire phone, but clearly the .Current property is clearly somehow specific to each project. I noticed that that its of type MediaPlayer, so is there any way I could place the object in a global container that I can access from both tasks?

Thanks in advance

Ali250
  • 652
  • 1
  • 5
  • 19
  • I don't know much about 8.1 background tasks, but I would guess they execute in individual processes. You may have to use some inter-process mechanism to communicate, e.g. named pipes, sockets, shared memory, etc. (not sure if that last one is even supported for store apps...it might not be). – Peter Duniho Dec 26 '14 at 20:31
  • That's the issue. I've been searching the net for a while and I can't find *any* relevant literature on establishing communication between two background tasks in WP8.1 – Ali250 Dec 26 '14 at 20:47
  • Well, the techniques I mentioned will work equally well whether intra- or inter-process. I agree that the docs for XAML/store apps is pretty sparse, especially in terms of practical guidance. And I don't know first-hand that there isn't a better way. But for sure, even if they aren't the best way, these techniques should work. – Peter Duniho Dec 26 '14 at 20:51

1 Answers1

0

I only answer this in case someone like me needs to still develop for WP8.1.

The only way I can think of doing this is to have the two background tasks in the same solution and use ApplicationData.Current.LocalSettings to save a value into settings, and pull the changes at an interval from the settings in the music player task.

Basically the background audio task keeps an eye on the settings to see if the Bluetooth background task changes them and then responds by pausing or playing the audio.

TheAgent
  • 1,472
  • 5
  • 22
  • 42