I have app with player like in this example.
More and more users report same problem: music continue to play in background when they receive call.
I have found some solution with observer here, question is how to implement this in B4i.
My main goal is to pause player in background on call (incoming and outgoing).
@objc func playerInterruption(notification: NSNotification) {
guard let userInfo = notification.userInfo,
let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
let type = AVAudioSession.InterruptionType(rawValue: typeValue) else {
return
}
if type == .began {
self.MusicPauseMethod()
}else if type == .ended {
self.MusicPlayMethod()
}
}
The previous code I posted was wrong. Untested code:
B4X:
Private Sub Application_Start (Nav As NavigationController)
NavControl = Nav
Page1.Initialize("Page1")
Page1.RootPanel.LoadLayout("Page1")
NavControl.ShowPage(Page1)
Me.As(NativeObject).RunMethod("addInterruption", Null)
End Sub
Private Sub Interruption_Notification(Notif As Object)
Dim State As Int = Notif.As(NativeObject).GetField("userInfo").RunMethod("valueForKey:", Array("AVAudioSessionInterruptionTypeKey")).AsNumber
Log(State)
End Sub
#if OBJC
- (void) addInterruption {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interruption:) name:AVAudioSessionInterruptionNotification object:nil];
}
- (void) interruption:(NSNotification*) notification {
NSLog(@"interruption");
[self.bi raiseEvent:nil event:@"interruption_notification:" params:@[notification]];
}
#end if