AVCaptureSession und AVAudioPlayer zusammen verwenden

Ich kann Videos aufnehmen und den Film korrekt in eine Datei ausgeben. Ich habe jedoch ein Problem mit der Videoaufnahme (kein Videoausgang), wenn ich versuche, mit AVAudioPlayer etwas Audio abzuspielen. Bedeutet das, dass ich AVCaptureSession und AVAudioPlayer nicht gleichzeitig verwenden kann? Hier ist mein Code zum Erstellen der Aufnahmesitzung und zum Abspielen des Audios. Die Videoaufnahme wird zuerst gestartet, dann möchte ich während der Aufnahme etwas Audio abspielen. Vielen Dank für jede Hilfe.

Code zum Erstellen der Aufnahmesitzung zum Aufnehmen von Videos (mit Audio) - Ausgabe in eine MOV-Datei:

- (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 zum Abspielen des Audios. Diese Funktion wird während der Videoaufnahmesitzung aufgerufen. Wenn ich diese Funktion nicht aufrufe, wird das Video aufgezeichnet und ich kann es in der MOV-Datei speichern. Wenn ich diese Funktion aufrufe, gibt es jedoch keine MOV-Ausgabedatei.

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

Antworten auf die Frage(1)

Ihre Antwort auf die Frage