简体   繁体   中英

Recording Audio on iOS Without Stopping Music Playback

When recording audio with an AVAudioSession audio playback from the music app or others is stopped. This is described in the audio session documentation:

AVAudioSessionCategoryRecord

For recording audio; this category silences playback audio.

Is there any way to change this behavior and do one of the following?

1) Continue playing other apps' audio and allow the phone to record the audio that is being played.

2) Resume other apps' audio once a brief recording is finished. There are notifications apps can respond to when audio interruptions begin or end. Is there something specific that must be done for other apps to receive these notifications?

from 24 hours of diligent searching and piecemealing things together, this is surprisingly simple:

let session = AVAudioSession.sharedInstance()
session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.mixWithOthers, .allowBluetoothA2DP])

And that's it for anyone still searching how to do this.

You can use

Obj-C

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];

or

Swift

let audioSession = AVAudioSession.sharedInstance()
        do {
            try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord,
                                         with: AVAudioSessionCategoryOptions.mixWithOthers)
        } catch let error as NSError {
            print("audioSession error: \(error.localizedDescription)")
        }

1) Not possible for AVAudioSession . All you can do is check [[AVAudioSession sharedInstance] isOtherAudioPlaying] and ask user would he like to stop music streaming or not.

2) App can't receive any notifications when iPod state changed, we can't even observe isOtherAudioPlaying property... Solution I use on my projects: add a NSTimer and once in sec check [[AVAudioSession sharedInstance] isOtherAudioPlaying] BOOL value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM