Аудио заставит AVCaptureSession прекратить - PullRequest
5 голосов
/ 17 января 2011

Я написал приложение для записи видео с iPhone.Это работает нормально, но есть одна большая проблема.Когда AVCaptureSession запускается, и пользователь пытается воспроизвести аудио из своей библиотеки (iPod).Это действие приведет к прекращению AVCaptureSession.Кто-нибудь может помешать пользователю попытаться воспроизвести аудио или решить эту проблему?


это мой код:

videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];           
audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];

AVCaptureDeviceInput *videoDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:videoDevice error:nil];
AVCaptureDeviceInput *audioDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:audioDevice error:nil];

movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

captureSession = [[AVCaptureSession alloc] init];

[captureSession beginConfiguration];
[captureSession setSessionPreset:AVCaptureSessionPresetHigh];
[captureSession addInput:videoDeviceInput];
[captureSession addInput:audioDeviceInput];
[captureSession addOutput:movieFileOutput];
[captureSession commitConfiguration];

[captureSession startRunning];

Ответы [ 3 ]

1 голос
/ 11 декабря 2012

Это сработало для меня:

- (void)setupAudio {
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
    UInt32 doSetProperty = 1;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

}

0 голосов
/ 01 августа 2012

Попробуйте возиться с аудио сеансом!

Вот краткое предположение о том, что вы могли бы сделать, но я специально не пробовал это с iPod:

OSStatus status = noErr;
status |= AudioSessionInitialize(CFRunLoopGetMain(), kCFRunLoopCommonModes, PVAudioSessionInterruptionListener, NULL);

    status |= AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(UInt32), &(UInt32){kAudioSessionCategory_PlayAndRecord});

    status |= AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers,
                                      sizeof(UInt32),
                                      &(UInt32){true});

    status |= AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck,
                                      sizeof(UInt32),
                                      &(UInt32){false});

status |= AudioSessionSetActive(YES);

if (status != noErr) {
    NSLog(@"ERROR: There was an error in setting the audio session");
}

Мне также повезло с категорией звукового окружающего звука (хотя она выдает ошибку, кажется, что она позволяет воспроизводить звук во время записи видео):

    status |= AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(UInt32), &(UInt32){kAudioSessionCategory_AmbientSound});
0 голосов
/ 14 ноября 2011

Вам необходимо использовать NSNotificationCenter. Используйте приведенный ниже код (я также включил некоторые другие полезные методы) и напишите метод для AVCaptureSessionWasInterruptedNotification, который будет обрабатывать прерывание захвата любым способом. Я надеюсь, что это помогает.

NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
[notify addObserver: self selector: @selector(onVideoError:) name: AVCaptureSessionRuntimeErrorNotification object: captureSession];
[notify addObserver: self selector: @selector(onVideoInterrupted:) name: AVCaptureSessionWasInterruptedNotification object: captureSession];
[notify addObserver: self selector: @selector(onVideoEnded:) name: AVCaptureSessionInterruptionEndedNotification object: captureSession];
[notify addObserver: self selector: @selector(onVideoDidStopRunning:) name: AVCaptureSessionDidStopRunningNotification object: captureSession];
[notify addObserver: self selector: @selector(onVideoStart:) name: AVCaptureSessionDidStartRunningNotification object: captureSession];
...