在iOS中录制视频时播放音频文件

6

我想在录制视频时播放音频文件。我使用AVAudioPlayer来播放音频,使用AVCamCaptureManager来录制视频。

但是当音频开始播放时,预览屏幕会冻结。我该怎么办?

谢谢你的帮助。这是代码(我正在使用AVCam示例)。

这是预览部分。

if ([self captureManager] == nil) {
        AVCamCaptureManager *manager = [[AVCamCaptureManager alloc] init];
        [self setCaptureManager:manager];

        [[self captureManager] setDelegate:self];

        if ([[self captureManager] setupSession]) {             
            AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:[[self captureManager] session]];
            UIView *view = [self videoPreviewView];
            CALayer *viewLayer = [view layer];
            [viewLayer setMasksToBounds:YES];

            CGRect bounds = CGRectMake(0, 0, 480, 320);
            [newCaptureVideoPreviewLayer setFrame:bounds];

            if ([newCaptureVideoPreviewLayer isOrientationSupported]) {
                [newCaptureVideoPreviewLayer setOrientation:[DeviceProperties setPreviewOrientation]];
            }

            [newCaptureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

            [viewLayer insertSublayer:newCaptureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];

            [self setCaptureVideoPreviewLayer:newCaptureVideoPreviewLayer];
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                [[[self captureManager] session] startRunning];
            });

            [self updateButtonStates];          
        }

这是音频播放部分。
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", sound] ofType:@"wav"]];
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [audioPlayer play];

如果我使用AudioServicesPlaySystemSound normal而不冻结,但这次它只在viewDidLoad上工作。
CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileRef;
    soundFileRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) CFBridgingRetain(@"sound"), CFSTR ("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

你能发一下你的代码吗? - Robert
这个有更新吗?我在想,是否可以在使用AVAudioPlayer录制音频时打开相机(不一定要录制视频)? - Jay Q.
我也有同样的问题。现在我该怎么办? - Arjun Sa
2个回答

1

谢谢你 谢谢你 谢谢你 谢谢你 谢谢你 谢谢你 谢谢你 - izzy
1
没问题,@izzy! - C0D3

0
希望将 AVAudioSession 类别设置为 AVAudioSessionCategoryPlayAndRecord 可以解决问题。同时,将其类别选项设置为 AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers。请查看下面的代码集合。
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

NSURL *soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"VoicemailSound" ofType:@"mp3"]]; //Download and add a system sound to your project and mention its name and type here
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers error: nil];

[self.audioPlayer prepareToPlay];
[self.audioPlayer play];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接