【问题标题】:Using AVCaptureSession and AVAudioPlayer together一起使用 AVCaptureSession 和 AVAudioPlayer
【发布时间】:2012-06-09 20:07:52
【问题描述】:

我能够录制视频并将电影正确输出到文件。但是,在尝试使用 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];
}

【问题讨论】:

    标签: iphone ios ipad avfoundation


    【解决方案1】:

    我通过设置音频会话解决了这个问题。在创建音频播放器对象来播放音频之前,我调用了以下函数。这样,我就能够同时录制视频(带音频)和播放音频。

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

    【讨论】:

    • 嗨,HappyAppDeveloper,我遇到了同样的问题。我在 setupAudioSession 中做同样的初始化,但我的捕获会话总是停止。你知道这是否适用于 iOS6 吗?
    • 如果您使用 ARC,则必须将音频播放器声明为“强”。否则它可能会提前发布,你不会得到任何声音。
    • 感谢它的工作。我还必须将 AudioToolbox 框架添加到我的项目中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多