【问题标题】:Audio will make AVCaptureSession terminate音频将使 AVCaptureSession 终止
【发布时间】:2011-01-17 07:39:58
【问题描述】:

我编写了一个应用程序来从 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];

【问题讨论】:

  • 您找到解决方案了吗?
  • 你找到解决方案了吗@anistar?
  • 不幸的是我遇到了同样的问题 - 有什么想法吗?

标签: iphone audio avcapturesession


【解决方案1】:

这对我有用:

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

}

【讨论】:

  • 你是如何将这个添加到你的 avcapturesession 中的,你能提供更多代码吗?
  • 抱歉,我已经有好几年没有看到这个了,无法提供任何真正的进一步背景信息。
【解决方案2】:

您需要使用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];

【讨论】:

  • 但是你应该停止或保存什么样的事情以确保它继续工作?
【解决方案3】:

试着弄乱音频会话!

这里有一个关于你可以做什么的快速猜测,但我没有专门用 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});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 2012-05-08
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 2012-02-28
    • 2011-03-30
    相关资源
    最近更新 更多