iOS Question [SOLVED] Hide Mediaplayer on lockscreen

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hello

This may seem like a strange request, but how do I hide the media player on the lock screen. Currently I pop a notification up using UserNotificationCenter Class, and also play a looping sound with the media player. This is intention that the user needs to perform a function in the app, so a single notification beep is not sufficient. Or maybe is is possible to play a looping custom sound on the notification ?

Regards

John
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
How will it work when the app is not running?
This is not relevant to the application. If the app is not running then the Notification is not produced. All notifications are local, generated by the app itself.

Are you sending silent push notifications?
No.

Are you using the audio background mode?

Yes
B4X:
#PlistExtra: <key>UIBackgroundModes</key><array><string>location</string><string>audio</string></array>

Example of a func that creates notification if the app is in the background and plays audio,
B4X:
Sub start_warning
    
    ' // do notification
    If tmrBackground.Enabled Then
        UNC.CreateNotificationWithContent("Health Check","Are you OK ?", "healthcheck_1", "", 1000)        
    End If
    
    mpOK.Initialize(File.DirAssets,"ok_chime_1.mp3","")    
    mpOK.looping = True
    mpOK.play
    
    tmrCheckShake.Enabled = True
    inWarning = True
    mod_functions.writeLog($"main(), start_warning ${inWarning}"$)
    
        
    CallSubDelayed(Me,"confirmOK")
    
End Sub

Regards

John.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Try this:
Set the audio session category to AVAudioSessionCategoryPlayback with options set to: AVAudioSessionCategoryOptionDuckOthers

Example of setting the session: https://www.b4x.com/android/forum/threads/audio-over-bluetooth.90191/post-570386

Thanks Erel, that did the trick.

For others if needed
B4X:
- (void)setAudioSession {
  AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  NSError *err = nil;
  BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers error:&err];
  if (success) {
     success = [audioSession setActive:YES error:&err];
   success = [audioSession setPreferredSampleRate:4096 error:nil];
   [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  }
  if (!success)
  [NSException raise:@"" format:@"Error setting audio session: %@", err];
}
 
Last edited:
Upvote 0
Top