iphone – 一起使用AVCaptureSession和AVAudioPlayer

iphone – 一起使用AVCaptureSession和AVAudioPlayer,第1张

概述我可以录制视频并将影片输出到文件中.但是,当尝试使用AVAudioPlayer播放某些音频时,我有视频录制问题(无视频输出).这是否意味着我不能同时使用AVCaptureSession和AVAudioPlayer?这是我的代码来创建捕获会话并播放音频.视频拍摄首先启动,然后在拍摄期间,我想播放一些音频.非常感谢任何帮助. 代码创建捕获会话录制视频(带音频) – 输出到.mov文件: - (void 我可以录制视频并将影片输出到文件中.但是,当尝试使用AVAudioPlayer播放某些音频时,我有视频录制问题(无视频输出).这是否意味着我不能同时使用AVCaptureSession和AVAudioPlayer?这是我的代码来创建捕获会话并播放音频.视频拍摄首先启动,然后在拍摄期间,我想播放一些音频.非常感谢任何帮助.

代码创建捕获会话录制视频(带音频) – 输出到.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];}

代码播放音频,此功能在视频拍摄会话期间进行呼叫.如果我不调用此功能,录像将被录制,我可以保存到.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];}
解决方法 我通过设置音频会话来解决问题.在创建音频播放器对象播放音频之前,我调用了以下功能.这样,我能够同时记录视频(带音频)和播放音频.

- (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];    }
总结

以上是内存溢出为你收集整理的iphone – 一起使用AVCaptureSession和AVAudioPlayer全部内容,希望文章能够帮你解决iphone – 一起使用AVCaptureSession和AVAudioPlayer所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1083407.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存