一起使用AVCaptureSession和AVAudioPlayer
我能够录制video并正确输出电影到文件。 但是,在尝试使用AVAudioPlayer播放某些audio时,我遇到了录像问题(无video输出)。 这是否意味着我不能同时使用AVCaptureSession和AVAudioPlayer? 这里是我的代码来创build捕获会话和播放audio。 video捕捉首先启动,然后在捕捉过程中,我想播放一些audio。 非常感谢您的帮助。
创build捕获会话以loggingvideo(带audio)的代码 – 输出到.mov文件:
- (void)addVideoInput AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; //... also some code for setting up videoDevice/frontCamera NSError *error; AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:frontCamera error:&error]; AVCaptureDeviceInput *audioIn = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error]; if (!error) { if ([captureSession canAddInput:audioIn]) [captureSession addInput:audioIn]; else NSLog(@"Couldn't add audio input."); if ([captureSession canAddInput:videoIn]) [captureSession addInput:videoIn]; else NSLog(@"Couldn't add video input."); } - (void)addVideoOutput { AVCaptureMovieFileOutput *m_captureFileOutput = [[AVCaptureMovieFileOutput alloc] init]; [captureSession addOutput:m_captureFileOutput]; [captureSession startRunning]; NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSMutableString *filePath = [NSMutableString stringWithString:@"Movie.mov"]; NSString* fileRoot = [docDir stringByAppendingPathComponent:filePath]; NSURL *fileURL = [NSURL fileURLWithPath:fileRoot]; AVCaptureConnection *videoConnection = NULL; for ( AVCaptureConnection *connection in [m_captureFileOutput connections] ) { for ( AVCaptureInputPort *port in [connection inputPorts] ) { if ( [[port mediaType] isEqual:AVMediaTypeVideo] ) { videoConnection = connection; } } } [videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; [m_captureFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self]; [m_captureFileOutput release]; }
代码播放audio,这个function是在video捕捉过程中调用的。 如果我不调用这个function,video被logging下来,我可以保存到.mov文件。 但是,如果我调用这个函数,没有输出.mov文件。
- (void)playAudio { NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"AudioFile" ofType:@"mp3"]; NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath]; AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; [fileURL release]; self.audioPlayer = newPlayer; [newPlayer release]; [audioPlayer setDelegate:self]; [audioPlayer prepareToPlay]; [audioPlayer play]; }
我通过设置audio会话解决了这个问题。 在创buildaudio播放器对象播放audio之前,我调用了以下函数。 这样,我就能够录制video(带audio)并同时播放audio。
- (void)setupAudioSession { static BOOL audioSessionSetup = NO; if (audioSessionSetup) { return; } [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil]; UInt32 doSetProperty = 1; AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty); [[AVAudioSession sharedInstance] setActive: YES error: nil]; audioSessionSetup = YES; } - (void)playAudio { [self setupAudioSession]; NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"AudioFile" ofType:@"mp3"]; NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath]; AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; [fileURL release]; self.audioPlayer = newPlayer; [newPlayer release]; [audioPlayer setDelegate:self]; [audioPlayer prepareToPlay]; [audioPlayer play]; }
- 适用于带有AVFoundation的AVAudioUnitSampler的正确音量信封
- 使用AVMutableAudioMix调整资产内曲目的音量
- 捕获的照片用AVCaptureSession sessionPreset = AVCaptureSessionPresetPhoto拉伸
- IOS / iPhone的照片突发模式API
- 为什么Swift的AVPlayer会在一次播放中加载playerItem两次?
- kCVPixelFormatType_420YpCbCr8BiPlanarFullRange帧到UIImage转换
- 我如何从UIImage中提取AVMetadataObject?
- iOS 7 AVCaptureMetadataOutput委托(QRCode扫描器)
- 如何将任何颜色的色度键filter应用于相机馈送ios?