iOS Question How to pause background music on incoming call

Pendrush

Well-Known Member
Licensed User
Longtime User
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).
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Start with this code in the main module:
B4X:
#if OBJC
@end
@interface B4IViewController (Interruption)
@end
@implementation B4IViewController (Interruption)
- (void) addInterruption {
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interruption:) name:AVAudioSessionInterruptionNotification object:nil];
}
- (void) interruption:(NSNotification*) notification {
    NSLog(@"test");
}
#end if
And you need to add the listener with:
B4X:
B4XPages.GetNativeParent(Me).As(NativeObject).RunMethod("addInterruption", Null)
Or with the Page object if not B4XPages.

Do you see the "test" log?
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
Im getting error: Main - 236: Unknown member: getnativeparent
B4X:
Private pg As Page
pg.GetNativeParent(Me).As(NativeObject).RunMethod("addInterruption", Null)
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
It shoud be:
B4X:
pg.As(NativeObject).RunMethod("addInterruption", Null)

I can see TEST in log when call device.
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
I need to implement this:

Swift:
@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()
    }
}

to be able to pause and resume player.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I can see TEST in log when call device.
That's good.

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
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…