Wish Background play and pause from outside the app

valentino s

Active Member
Licensed User
Longtime User
To play audio in background mode I've used the method described here: https://www.b4x.com/android/forum/threads/background-playback.49337/

It works.

But when i'm outside the app I can rise up from bottom the panel that let me pause and play volume. It doesn't work, as if the app wasn't registered as the currently playing an audio. I used videoview to play the audio.

I've missed something or there's something more to add, perhaps something more in plistextra ?

This lnk may help: on "didTapPlayPause" from here https://www.raywenderlich.com/29948/backgrounding-for-ios

"Now all that’s remaining is for you to add the implementation of didTapPlayPause to get the play/pause button working. Add this new method at the bottom of TBFirstViewController.m, just above @end:

- (IBAction)didTapPlayPause:(id)sender
{
self.btnPlayPause.selected = !self.btnPlayPause.selected;
if (self.btnPlayPause.selected)
{
[self.player play];
}
else
{
[self.player pause];
}
}
"


Thanks,
 
Last edited:

valentino s

Active Member
Licensed User
Longtime User
Just to highlight how to support the bottom drawer, as Erel stated in #4:

Put in application_start

B4X:
NativeMe.RunMethod("setAudioSession", Null)
NativeMe.RunMethod("register", Null)

then this part supports the play/stop

B4X:
Public Sub ControlEvent (Command As String)
  Select Command
  Case "play"
  vv.Play
  Case "pause"
  vv.Pause
  End Select
End Sub

and in OBJC:

B4X:
- (void)register {
  MPRemoteCommandCenter* center = [MPRemoteCommandCenter sharedCommandCenter];
  center.playCommand.enabled = true;
  center.pauseCommand.enabled = true;
  [center.playCommand addTarget:self action:@selector(play)];
  [center.pauseCommand addTarget:self action:@selector(pause)];
}
- (void) play {
  NSLog(@"test");
  [self.bi raiseEvent:nil event:@"controlevent:"  params:@[@"play"]];
}
- (void) pause {
  [self.bi raiseEvent:nil event:@"controlevent:"  params:@[@"pause"]];
}

It is very useful to understand how b4i works with ios.
 
Top