Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

iphone - Using AVCaptureSession and AVAudioPlayer together

I am able to record video and output the movie to a file correctly. However, I have a problem with video recording (no video ouput) when trying to use AVAudioPlayer to play some audio. Does it mean that I cannot use AVCaptureSession and AVAudioPlayer at the same time? Here is my code to create the capture session and to play the audio. The video capture is started first, then during the capturing, I want to play some audio. Thanks so much for any help.

Code to create the capture session to record video (with audio) - output to a .mov file:

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

Code to play the audio, this function is call during the video capture session. If I don't call this function, the video is recorded and I am able to save to the .mov file. However, if I call this function, there's no output .mov file.

- (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];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I fixed the problem by setting up the audio session. I called the following function before creating the audio player object to play the audio. That way, I was able to record video (with audio) and play audio at the same time.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...