使用 iPhone SDK 同时播放和录制声音?
我正在尝试检测来自麦克风的输入并播放声音,这些声音可以打开或关闭,但在麦克风侦听器处于活动状态时始终播放。
我已经在 viewDidLoad 方法中初始化了我的 AVAudioSession ,如下所示:
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *err = nil;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];
[session setActive:YES error:&err];
然后设置应该播放的声音:
NSURL *activeUrl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audio_file.caf", [[NSBundle mainBundle] resourcePath]]];
activePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:activeUrl error:nil];
activePlayer.numberOfLoops = -1;
activePlayer.currentTime = 0;
activePlayer.volume = -1.5;
我正在使用 SCListener 类来进行录音。当我在手机上运行此命令时,录音机工作正常,但即使我在 viewDidLoad 方法中调用播放,声音也不会循环。
有什么想法吗?
I'm trying to detect input from the mic and also play sounds, which can be toggled on or off, but are always playing when the mic listener is active.
I've initialized my AVAudioSession in the viewDidLoad method like so:
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *err = nil;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];
[session setActive:YES error:&err];
Then setup the sound that's supposed to be played:
NSURL *activeUrl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audio_file.caf", [[NSBundle mainBundle] resourcePath]]];
activePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:activeUrl error:nil];
activePlayer.numberOfLoops = -1;
activePlayer.currentTime = 0;
activePlayer.volume = -1.5;
And I'm using the SCListener class to do the recording. When I run this on the phone, the recorder works just fine, but the sound won't loop even when I call play for it in the viewDidLoad method.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
非常感谢您的回复。我确实在不久前就弄清楚了这一点,但从未抽出时间来更新。结果发现非常简单。您必须将 AVAudioSession 的类别设置为 AVAudioSessionCategoryPlayAndRecord。
几乎所有您需要的文档都在类参考中:
http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html
Thanks so much for the responses. I did actually figure this out a while back, but never got around to updating this. Turned out to be very simple. You have to set category of AVAudioSession to AVAudioSessionCategoryPlayAndRecord.
Pretty much all the documentation you need is here in the class reference:
http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html
我遇到了与您类似的问题 - 代码在模拟器上有效,但在设备上无效。我在使用 Cocos2D (Denshion) 以及 AVAudioRecorder 和 AVAudioRecorder 时遇到了这个问题。 AVAudioPlayer。
主要问题似乎是 AVAudioRecorder 和 AVAudioPlayer 代表您设置
AVAudioSessionCategory
,但如果您自己操作它,它们似乎会记住您正在“自我管理”它并停止为您管理它。请参阅这篇博文,它对我有帮助(尽管我仍然我自己还没有解决这个问题;当我解决这个问题时我会更新这篇文章)。
I had a similar issue to you - the code worked on the simulator, but not on the device. I had this problem using Cocos2D (Denshion) along with AVAudioRecorder & AVAudioPlayer.
The main issue seems to be that AVAudioRecorder and AVAudioPlayer set the
AVAudioSessionCategory
on your behalf, but if you manipulate it yourself, they seem to remember that you're "self managing" it and stop managing it for you.See this blog post that helped me (although I still haven't solved the issue myself; I'll update this post when I do solve it).