iPhone SDK:AVAudioRecorder在调用[AVPlayer播放]后不会录音
我有一个使用 AVPlayer 的视图控制器。该视图控制器可以加载模态视图控制器,用户可以在其中使用 AVAudioRecorder 录制音频。
这就是发生的情况:
如果用户使用 [AVPLayer play] 在第一个控制器中播放乐曲,AVAudioRecorder 将不会在模态视图控制器中录制。没有错误,但 AVAudioRecorder 返回的当前时间是 0.0;
如果用户关闭模式对话框并重新加载它。 AVAudioRecorder 工作正常。
这可以一遍又一遍地重复 AVAudioRecorder 在 [AVPlayer play] 调用后第一次调用时不起作用,
我已经为此奋斗了好几天,刚刚重新组织了与 AVPlayer 和 AVAudioRecorder 相关的代码,但它仍然表现得很奇怪。
非常感谢任何帮助或指示
提前致谢
Jean-Pierre
I have a view controller that uses AVPlayer. this view controller can load a modal view controler where the user can record audio using AVAudioRecorder.
this is what happens:
if the user plays the composition in the fist controller with [AVPLayer play] the AVAudioRecorder will not record in the modal view controller. there are no errors but the current time returned by AVAudioRecorder is 0.0;
if the user dismisses the modal dialog and reloads it. AVAudioRecorder works fine.
this can be repeated over and over
AVAudioRecorder does not work the first time it is invoked after a [AVPlayer play] call
I have been fighting with this for days and just have reorganized my code related to both AVPlayer and AVAudioRecorder and it's still acting weird.
Any help or pointer is much appreciated
Thanks in advance
Jean-Pierre
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我也遇到了同样的问题,这就是我找到的解决方案。
录音时,在
AVAudioRecord
初始化后写入这行代码:[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:NULL];
然后调用
record
方法。I've been having the same problem too, and this is the solution I found.
When recording, write this line code after
AVAudioRecord
is initialized:[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:NULL];
Then invoke
record
method.您如何设置
AVAudioSession
的类别,即播放或录制?尝试类似的东西How are you setting the
AVAudioSession
's category, i.e., play or record? Try something like我也遇到了同样的问题。我使用 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.
如果您需要同时使用 AVPlayer 和 AVAudioRecorder,请执行以下操作:
如果你不提供这个,播放将开始,但录音不会开始。
If you need use AVPlayer and AVAudioRecorder at the same time do following:
If you don't provide this, playback will start, but recording will not start.
不应该是
[AVAudioRecorder record];
而不是play
吗?shouldn't it be
[AVAudioRecorder record];
instead ofplay
?我遇到了同样的问题:AVAudioRecorder 没有开始录音。
我的示例有点不同:我有一个基于选项卡栏的应用程序,在不同的视图中包含多个 AVAudioPlayer。为了便于举例,假设第一个加载的视图有 3 个 AVAudioPlayer,而第二个视图有一个 AVAudioPlayer 和一个 AVAudioRecorder。它们都是在viewDidLoad方法中发起的。
录音机在第二个视图中无法工作。当我按下录制按钮时,视图轻微晃动,并且录制没有开始。
奇怪的是,模拟器工作正常并且没有表现出相同的症状...(有人知道为什么吗?)
经过一些研究和阅读这篇文章,我意识到我没有将第一个视图中的玩家设置为零,所以我猜测它们仍在内存中,阻止录音机启动。一旦我在 viewDidDisappear 方法中将它们设置为 nil ,它就再次正常工作了。
这对我有帮助:
ViewDidUnload 没有帮助,因为切换视图时,视图以及类本身中的变量没有被卸载。
希望这对其他人也有用。
I've had the same issue: AVAudioRecorder did not start recording.
My example is a bit different: I have a tabbar based app with several AVAudioPlayers in the different views. For the sake of the example, lets say that the first loaded view has 3 AVAudioPlayers, while the second view has one AVAudioPlayer and one AVAudioRecorder. They are all initiated in the viewDidLoad method.
The recorder was not working in second view. When I hit my record button, the view slightly shaked and the recording did not begin.
Strangely, the simulator worked fine and did not show the same symptoms... (anyone has an idea why?)
After some research and reading this thread I have realized that I have not set the players in the first view to nil, so I guess they were still in the memory preventing the recorder to start. Once I have set them to nil in the viewDidDisappear method, it was working alright again.
here is what helped me:
ViewDidUnload did not help because the View, and therefore the variablbes in the class itself were not unloaded when switching views.
Hope this may be useful for others as well.
@Aaron Hayman 的解释是准确的,但对我来说解决方案非常简单。只需实现播放器委托的 didFinishPlaying 方法并在那里释放播放器即可。
@Aaron Hayman explanation is accurate but the solution for me was very simple. Just implement the didFinishPlaying method of the player delegate and release the player there.
当我开发一个播放音频文件和录制用户声音的应用程序时,我遇到了同样的问题。我尝试了上面的解决方案,例如 AVPlayer 实例的 deinit,但这没有帮助。就我而言,解决方案是在使用 AVAudioRecorder 之前停用并激活 AVAudioSession 实例。我使用了这样的函数:
所以,用法是这样的:
When I was developing an app with playing audio files and recording user's voice I've encountered the same problem. I tried out solutions from above like deinit of AVPlayer instance, but that didn't help. In my case the solution was in deactivating and then activating an instance of AVAudioSession just before the use of AVAudioRecorder. I used functions like:
So, the usage was like this: