iOS Question How to let Music play in background while app makes sounds

Turbo3

Active Member
Licensed User
Longtime User
Since B4i does not support the B4A Beep function I needed to use Mediaplayer to make sounds.

My TestFlight testers have reported that when my app makes sounds the music they were playing in the background stops.

How can I allow background music and still make sounds in the app?

Or if the music must stop while the app makes a sound how can my app restart the background music they were previously playing?

As a quick fix, for now I have added a setting to disable the app from making any sounds.
 

Turbo3

Active Member
Licensed User
Longtime User
Did not work. I already have a routine called setAudioSession so I renamed this new one to setAudioSession1. Here is the code I already had:
B4X:
#If OBJC
#import <AVFoundation/AVFoundation.h>
- (void) setAudioSession {
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    BOOL ok;
    NSError *setCategoryError = nil;
    ok = [audioSession setCategory:AVAudioSessionCategoryPlayback
      error:&setCategoryError];
    if (!ok) {
          NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
    }
}
#end if

Do I need to combine the two routines into one?
 
Upvote 0

Turbo3

Active Member
Licensed User
Longtime User
Actually I switched the order of execution so the new code was executed last and it works but the old code that disabled looking at the ringer silence switch not longer works.

So it looks like we need to combine these two into one routine.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
That is correct.

Use this code:
B4X:
#If OBJC
#import <AVFoundation/AVFoundation.h>
- (void) setAudioSession {
  AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  BOOL ok;
  NSError *setCategoryError = nil;
  ok = [audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers
  error:&setCategoryError];
  if (!ok) {
  NSLog(@"%s setCategoryError=%@", __PRETTY_FUNCTION__, setCategoryError);
  }
}
#end if
 
Upvote 0
Top