【问题标题】:Play and record sound simultaneously with iPhone SDK?使用 iPhone SDK 同时播放和录制声音?
【发布时间】:2010-07-06 16:46:02
【问题描述】:

我正在尝试检测来自麦克风的输入并播放声音,这些声音可以打开或关闭,但始终在麦克风监听器处于活动状态时播放。

我已经在 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 方法中调用它,声音也不会循环播放。

有什么想法吗?

【问题讨论】:

    标签: iphone audio avaudioplayer record


    【解决方案1】:

    非常感谢您的回复。我确实在一段时间前确实想到了这一点,但从未有时间更新它。结果很简单。您必须将 AVAudioSession 的类别设置为 AVAudioSessionCategoryPlayAndRecord。

    您需要的几乎所有文档都在类参考中:

    http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题 - 代码在模拟器上运行,但在设备上运行不正常。我在使用 Cocos2D (Denshion) 以及 AVAudioRecorder 和 AVAudioPlayer 时遇到了这个问题。

      主要问题似乎是 AVAudioRecorder 和 AVAudioPlayer 代表您设置了AVAudioSessionCategory,但如果您自己操作它,它们似乎会记住您是“自我管理”它并停止为您管理它。

      请参阅对我有帮助的 this blog post(尽管我自己还没有解决这个问题;当我解决这个问题时,我会更新这篇文章)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多