使用AVPlayer播放视频并同时使用AVAudioRecorder录制声音 [英] Playing video with AVPlayer and recording sound with AVAudioRecorder simultaneously

查看:197
本文介绍了使用AVPlayer播放视频并同时使用AVAudioRecorder录制声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌上搜索这个问题很多天但是我没有找到解决我的简单问题的方法。

I was googling this problem for many days but I didn't find a solution for my simple problem.

我正在尝试播放视频而录制声音。没有视频,录制声音效果很好。视频在没有录制的情况下工作正常。

I'm trying to play a video while recording sound. Recording sound works fine without the video. And the video works fine without the recording.

只要我将两者放在一起,录制就会抛出对声音没有反应且视频不再播放的值。
顺便说一下,这只发生在我的设备上(iPhone 4 / iOS5)。在模拟器中一切正常。

As soon as I put both together, the recording throws values which do not react on sound and the video is not playing anymore. By the way this happens only on my device (iPhone 4 / iOS5). In the simulator everything works fine.

这是我的代码的简化版本,它位于ViewController中。

This is a reduced version of my code which is in a ViewController.

// Audio Recorder

NSURL *nullUrl = [NSURL fileURLWithPath:@"/dev/null"];

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithFloat:44100.0],                 AVSampleRateKey,
                          [NSNumber numberWithInt:kAudioFormatAppleLossless], AVFormatIDKey,
                          [NSNumber numberWithInt:1],                         AVNumberOfChannelsKey,
                          [NSNumber numberWithInt:AVAudioQualityMax],         AVEncoderAudioQualityKey,
                          nil];

NSError *recorderError;
_recorder = [[AVAudioRecorder alloc] initWithURL:nullUrl settings:settings error:&recorderError];

if (_recorder)
{
    [_recorder prepareToRecord];
    _recorder.meteringEnabled = YES;
    [_recorder record];

    _levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(levelTimerCallback:) userInfo:nil repeats:YES];
}


// Video Player

NSURL *url = [[NSBundle mainBundle] URLForResource:@"Video"
                                      withExtension:@"mp4"
                                       subdirectory:nil];

_avPlayer = [[AVPlayer playerWithURL:url] retain];
_avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:_avPlayer] retain];

_avPlayerLayer.frame = self.view.layer.bounds;
[self.view.layer addSublayer:_avPlayerLayer];

[_avPlayer play];

记录器只输出一个在levelTimerCallback方法中读取的值。
该视频只播放一段短视频。

The recorder simply outputs a value which is read in the levelTimerCallback method. The video simply plays a short video.

我发现我必须为AVAudioSession设置一个类别。
如果我在应用程序启动后实现此代码,视频正在播放,但录制器仍会输出相同的值而不对声音作出反应。

I figured out that I have to set a category for the AVAudioSession. If I implement this code after app start, the video is playing but the recorder still outputs the same values without reacting to sound.

NSError *error = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
[audioSession setActive:YES error:&error];

我可以想象我可能要对AudioSessionSetProperty做些什么

I could imagine that I might have to do something with AudioSessionSetProperty

UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);    

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

但这并没有改变任何事情。
您是否知道我缺少什么或如何正确设置AudioSession?

But that doesn't change anything. Do you have an idea what I am missing or how to set up the AudioSession properly?

感谢您提供的所有提示!

Thank you for every hint on that!

干杯,
Raphael;)

Cheers, Raphael ;)

推荐答案

我遇到了同样的问题(除了我使用AVPlayer播放音频而不播放视频)。我建议您开始收听音频路由更改,并且当AVPlayer和AVAudioRecorder同时启动时,您将看到多次路由更改。

I had the same problem (except I'm using the AVPlayer to play audio and not video though). I suggest you start listening for audio routing changes and you will see the route change multiple times when the AVPlayer and AVAudioRecorder are started at the same time.

AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange,
                                audioRouteChangeListenerCallback,
                                self);

在回调中打印出类别和路线(kAudioSessionProperty_AudioRoute)。我可以让音频播放和录音同时工作的唯一方法是:

In the callback print out the category and route (kAudioSessionProperty_AudioRoute). The only way I could get the audio playback and record to work simultaneously was if:


  1. 音频会话是AVAudioSessionCategoryPlayAndRecord和kAudioSessionProperty_OverrideCategoryMixWithOthers属性是的。

  1. The audio session was AVAudioSessionCategoryPlayAndRecord and kAudioSessionProperty_OverrideCategoryMixWithOthers property was true.

我在录制之前启动了播放。调用AVAudioRecorder prepareToRecord后,在获得音频路径更改之前不要开始录制。即使你早先开始录音也没有失败,音频路线的变化似乎使录音失败。

I initiated the playback before the recording. After AVAudioRecorder prepareToRecord is called, don't start recording until you get the audio route change. Even though it does not fail if you start recording earlier, the audio route change seems to make the recording fail.

我尝试了其他组合,但这是唯一一次有效的组合。

I tried other combinations, but this is the only one that works every time.

这篇关于使用AVPlayer播放视频并同时使用AVAudioRecorder录制声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆