Using AVCaptureSession and AVAudioPlayer together

前端 未结 1 1770
感动是毒
感动是毒 2020-12-14 11:55

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

相关标签:
1条回答
  • 2020-12-14 12:47

    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];
        }
    
    0 讨论(0)
提交回复
热议问题