【问题标题】:Playing audio file while recording video in iOS在 iOS 中录制视频时播放音频文件
【发布时间】:2013-02-20 11:35:04
【问题描述】:

我想在用相机录制的同时播放音频文件。我使用 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 而不冻结,但这次它只适用于 viewDidLoad。

CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileRef;
    soundFileRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) CFBridgingRetain(@"sound"), CFSTR ("wav"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

【问题讨论】:

  • 你能发布你的代码吗?
  • 这方面有什么更新吗?我想知道您是否可以在使用 AVAudioPlayer 录制音频时打开相机(不一定是录制视频)。
  • 我也有同样的问题。我现在该怎么办..

标签: ios avaudioplayer


【解决方案1】:

我的问题不是视频,而是在通话中播放声音(voip 类型的应用程序),这个问题的答案对我有用:

Using vibrate and AVCaptureSession at the same time

【讨论】:

  • 谢谢谢谢谢谢谢谢谢谢谢谢谢谢
  • :) @izzy 没问题!
【解决方案2】:

希望将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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 2017-10-17
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多