麦克风输入在模拟器中工作,而不是在手机 xcode 上工作
我正在尝试编写一个应用程序,该应用程序使用麦克风的平均功率来触发游戏响应。我在 -init
中使用以下代码:
NSURL *url = [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,il];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(myMethod:) userInfo: nil repeats: YES];
在我的方法中 float soundLevel=[录音机averagePowerForChannel:0];
触发在模拟器中工作正常,但当我将其放在手机上进行测试时却无法工作。我做错了什么?
I am trying to program an app that uses average power to the mic to trigger a game response. I am using the following code in -init
:
NSURL *url = [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,il];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(myMethod:) userInfo: nil repeats: YES];
in my method
float soundLevel=[recorder averagePowerForChannel:0];
The triggering is working fine in the simulator but not working when i put it on my phone to test. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定
@"/dev/null"
是设备上的有效路径。您的应用程序无法在其沙箱之外读取或写入,因此设置 AVAudioRecorder 实例来录制到 /dev/null 可能不起作用。尝试为其提供应用程序文档文件夹中文件的路径。
I'm not sure that
@"/dev/null"
is a valid path on the device. Your app can't read or write outside its sandbox, so setting up an instance of AVAudioRecorder to record to /dev/null probably isn't going to work.Try giving it a path to a file in your app's Documents folder instead.