如果电影之前已录制且已录制,AVAudioRecorder 将不会录制玩过
我的 iPhone 应用程序使用“AVAudioRecorder”进行录音。它还使用“UIImagePickerController”来录制电影,使用“MPMoviePlayerController”来播放电影。
一切正常,直到我连续完成所有三件事:
- 使用 UIImagePickerController 录制电影
- 使用 MPMoviePlayerController 播放录制的电影
- 尝试使用 AVAudioRecorder 进行录音
当我在步骤 3 中调用 AVAudioRecorder 的“record”方法时,它返回 NO 表示失败,但没有给出任何提示为什么(来吧苹果!) AVAudioRecorder 的 audioRecorderEncodeErrorDidOccur 委托方法从未被调用,并且在设置录音机时我没有收到其他错误。
我的第一个猜测是电影录制/播放正在修改“AVAudioSession”的共享实例,从而阻止录音机工作。但是,我手动将 AVAudioSession 的类别属性设置为“AVAudioSessionCategoryRecord”,并在尝试录制之前使音频会话处于活动状态。
这是我创建录音机的方法:
- (void)createAudioRecorder
{
NSError *error = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:&error];
if (error)
...
[audioSession setActive:YES error:&error];
if (error)
...
NSMutableDictionary *settings = [[NSMutableDictionary alloc] init];
// General Audio Format Settings
[settings setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[settings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[settings setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
// Encoder Settings
[settings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];
[settings setValue:[NSNumber numberWithInt:96] forKey:AVEncoderBitRateKey];
[settings setValue:[NSNumber numberWithInt:16] forKey:AVEncoderBitDepthHintKey];
// Write the audio to a temporary file
NSURL *tempURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"Recording.m4a"]];
audioRecorder = [[AVAudioRecorder alloc] initWithURL:tempURL settings:settings error:&error];
if (error)
...
audioRecorder.delegate = self;
if ([audioRecorder prepareToRecord] == NO)
NSLog(@"Recorder fails to prepare!");
[settings release];
}
这是我开始录音的方法:
- (void)startRecording
{
if (!audioRecorder)
[self createAudioRecorder];
NSError *error = nil;
[[AVAudioSession sharedInstance] setActive:YES error:&error];
if (error)
...
BOOL recording = [audioRecorder record];
if (!recording)
NSLog(@"Recording won't start!");
}
以前有人遇到过这个问题吗?
My iPhone app uses "AVAudioRecorder" to make voice recordings. It also uses "UIImagePickerController" to record movies and "MPMoviePlayerController" to play movies.
Everything works fine until I do all three things in a row:
- Record a movie using UIImagePickerController
- Play back the recorded movie using MPMoviePlayerController
- Try to make a voice recording using AVAudioRecorder
When I call AVAudioRecorder's "record" method in step 3, it returns NO indicating failure, but giving no hints as to why (come on Apple!) AVAudioRecorder's audioRecorderEncodeErrorDidOccur delegate method is never called and I receive no other errors when setting up the recorder.
My first guess was that the movie recording/playing was modifying the shared instance of "AVAudioSession" in such a way that it prevented the audio recorder from working. However, I'm manually setting AVAudioSession's category property to "AVAudioSessionCategoryRecord" and I make the audio session active before trying to record.
Here's my method for creating the recorder:
- (void)createAudioRecorder
{
NSError *error = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:&error];
if (error)
...
[audioSession setActive:YES error:&error];
if (error)
...
NSMutableDictionary *settings = [[NSMutableDictionary alloc] init];
// General Audio Format Settings
[settings setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[settings setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[settings setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
// Encoder Settings
[settings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];
[settings setValue:[NSNumber numberWithInt:96] forKey:AVEncoderBitRateKey];
[settings setValue:[NSNumber numberWithInt:16] forKey:AVEncoderBitDepthHintKey];
// Write the audio to a temporary file
NSURL *tempURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"Recording.m4a"]];
audioRecorder = [[AVAudioRecorder alloc] initWithURL:tempURL settings:settings error:&error];
if (error)
...
audioRecorder.delegate = self;
if ([audioRecorder prepareToRecord] == NO)
NSLog(@"Recorder fails to prepare!");
[settings release];
}
And here's my method to start recording:
- (void)startRecording
{
if (!audioRecorder)
[self createAudioRecorder];
NSError *error = nil;
[[AVAudioSession sharedInstance] setActive:YES error:&error];
if (error)
...
BOOL recording = [audioRecorder record];
if (!recording)
NSLog(@"Recording won't start!");
}
Has anyone run into this problem before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我也有同样的问题。在我解决这个问题之前,我的录音/播放代码是这样的:
开始录音功能
停止录音功能
开始播放功能
停止播放功能
我修复了播放捕获视频后的录音问题,代码如下:
I was having the same issue. Before I fixed the issue, my recording/playback code was like this:
Start Recording Function
Stop Recording Function
Start Playing Function
Stop Playing Function
I fixed the recording issue after playing a captured video, the code is as follows:
有同样的问题。我的解决方案是在开始录制会话之前停止电影。我的代码与 rmomin 的代码类似。
Had the same problem. My Solution was to STOP the movie before starting the recording session. My code was similar to rmomin's code.
我也遇到了同样的问题。我使用 AVPlayer 来播放乐曲(之前的录音我使用过 AVAudioRecord)。然而,我发现一旦我使用了AVPlayer,我就无法再使用AVAudioRecorder了。经过一番搜索,我发现只要 AVPlayer 在内存中实例化并且至少播放过一次(通常是实例化后立即执行的操作),AVAudioRecorder 就不会录制。然而,一旦 AVPlayer 被释放,AVAudioRecorder 就可以再次自由录制。看来 AVPlayer 保留了 AVAudioRecorder 所需的某种连接,而且它很贪婪......它不会放开它,直到你从它冰冷的死手上撬开它。
这是我找到的解决方案。有些人声称实例化 AVPlayer 需要太多时间来不断崩溃和设置备份。然而,事实并非如此。实例化 AVPlayer 实际上非常简单。实例化 AVPlayerItem 也是如此。加载 AVAsset(或其任何子类)并不简单。你真的只想这样做一次。关键是使用这个序列:
** 请注意,销毁 AVPlayer 可能不仅仅需要释放它并将其 var 设置为 nil。最有可能的是,您还添加了一个周期性时间观察器来跟踪播放进度。当你这样做时,你会收到一个你应该抓住的不透明物体。如果您不从播放器中删除此项目并释放它/将其设置为 nil,AVPlayer 将不会释放。 Apple 似乎故意创建了一个必须手动打破的保留周期。因此,在销毁 AVPlayer 之前,您需要(示例):
作为旁注,您可能还设置了 NSNotifications(我使用一个来确定播放器何时完成播放)...也不要忘记删除它们。
I've been having the same problem. I use AVPlayer to play compositions (previous recordings I've used AVAudioRecord for). However, I found that once I've used AVPlayer I could no longer use AVAudioRecorder. After some searching, I discovered that so long as AVPlayer is instantiated in memory and has been played at least once (which is usually what you do immediately after instantiating it) AVAudioRecorder will not record. However, once AVPlayer is dealloc'd, AVAudioRecorder is then free to record again. It appears that AVPlayer holds on to some kind of connection that AVAudioRecorder needs, and it's greedy...it won't let it go until you pry it from it's cold dead hands.
This is the solution I've found. Some people claim that instantiating AVPlayer takes too much time to keep breaking down and setting back up. However, this is not true. Instantiating AVPlayer is actually quite trivial. So also is instantiating AVPlayerItem. What isn't trivial is loading up AVAsset (or any of it's subclasses). You really only want to do that once. They key is to use this sequence:
** Note that destroying AVPlayer may take more than just releasing it and setting its var to nil. Most likely, you've also added a periodic time observer to keep track of the play progress. When you do this, you receive back an opaque object you're supposed to hold on to. If you don't remove this item from the player AND release it/set it to nil, AVPlayer will not dealloc. It appears that Apple creates an intentional retain cycle you must break manually. So before you destroy AVPlayer you need to (example):
As a side note, you may also have set up NSNotifications (I use one to determine when the player has completed playing)...don't forget to remove those as well.
我在 Monotouch 中遇到了同样的问题,并调整了 Monotouch 的 rmomins 答案。
更改
为
Works like a charm。
I had the same problem in Monotouch and adjusted rmomins answer for Monotouch.
changed
to
Works like a charm.
我在录制和播放时遇到了同样的问题。为了解决这个问题,我回顾了“录制”和“播放”功能中的 AVAudioSession 。
这可能对在设备上而不是在模拟器上遇到此问题的人有所帮助!
I had the same problem to record and play. To fix the problem I recall AVAudioSession in the "record" and "play" function.
This can may be help to person who has this problem on the device and not on the simulator!
我遇到了同样的问题。最后我发现ARC已经发布了我的录音机。因此,您必须在 .h 文件中声明录音机,即
AVAudioRecord *recorder;
。将其他内容放入 .m 将正常工作。I got the same problem. Finally I found out that ARC has released my recorder. So, you must declare the recorder in your .h file, i.e.
AVAudioRecord *recorder;
. Put other things to .m will work as normal.