On continued playback of background music
With iOS default behavior, music playing in background playback is automatically stopped when playing back a movie while playing background music.
By adding the following processing, background music can be continuously played even during movie playback.
About designation method
Add the following processing.
- ObjectiveC
@import AVFoundation;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryAmbient error:nil];
1
2
3
4
2
3
4
- Swift
let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
if #available(iOS 10.0, *) {
do {
try audioSession.setCategory(.ambient, mode: .default)
} catch {
}
} else {
audioSession.perform(NSSelectorFromString("setCategory:error:"), with:AVAudioSession.Category.ambient)
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9