首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >音频将使AVCaptureSession终止

音频将使AVCaptureSession终止
EN

Stack Overflow用户
提问于 2011-01-17 07:39:58
回答 3查看 2.2K关注 0票数 5

我写了一个应用程序来录制来自iPhone的视频。它工作得很好,但有一个大问题。当AVCaptureSession开始运行时,用户尝试播放他们库(IPod)中的音频。此操作将使AVCaptureSession终止。有什么想法可以防止用户尝试播放音频或解决这个问题吗?

这是我的密码:

代码语言:javascript
复制
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];
EN

回答 3

Stack Overflow用户

发布于 2012-12-11 01:24:21

这对我起了作用:

代码语言:javascript
复制
- (void)setupAudio {
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
    UInt32 doSetProperty = 1;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

}

票数 1
EN

Stack Overflow用户

发布于 2011-11-14 16:33:18

您需要使用NSNotificationCenter。使用下面的代码(我还包括一些其他有用的方法),并为AVCaptureSessionWasInterruptedNotification编写一个方法,它将以任何方式处理捕获中断。我希望这能帮上忙。

代码语言:javascript
复制
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];
票数 0
EN

Stack Overflow用户

发布于 2012-08-01 08:14:43

尝试干扰音频会话!

下面是关于您可以做什么的快速猜测,但是我还没有在iPod中具体地尝试过这一点:

代码语言:javascript
复制
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");
}

我还对环境音频类别有了一些运气(尽管它给出了一个错误,它似乎允许在录制视频时播放音频):

代码语言:javascript
复制
    status |= AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(UInt32), &(UInt32){kAudioSessionCategory_AmbientSound});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4710977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档