AVCaptureSession and background audio iOS 7

后端 未结 2 1933
不思量自难忘°
不思量自难忘° 2020-11-29 05:35

Whenever I start an AVCaptureSession running with the microphone as an input it cancels whatever background music is currently running (iPod music for instance). If I commen

2条回答
  •  没有蜡笔的小新
    2020-11-29 06:13

    In your AppDelegate:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        //Stop app pausing other sound.
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
                                         withOptions:AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryOptionDefaultToSpeaker
                                               error:nil];
    }
    

    Where you are allocating AVCaptureSession:

    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    session.automaticallyConfiguresApplicationAudioSession = NO;
    

    This code will allow you to play background music and run AVCaptureSession with the microphone.

    SWIFT UPDATE:

    AppDelegate:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
        //Stop app pausing other sound.
        do{
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, 
                                                withOptions: [.DuckOthers, .DefaultToSpeaker])
        }
        catch {
    
        }
    
        return true
    }
    

    Where you are allocating AVCaptureSession:

    let session = AVCaptureSession()
    session.automaticallyConfiguresApplicationAudioSession = false
    

提交回复
热议问题