如何使用Unity和IOS来检测麦克风中的打击
|
我知道有多个线程存在相同的问题,但是我无法使其解决方案正常工作。我最终创建了这个课:
微控制器
#import Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#import <UIKit/UIKit.h>
@interface MicroController : UIView < UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, AVAudioSessionDelegate > {
AVAudioRecorder *recorder;
NSTimer *levelTimer;
double lowPassResults;
}
- (void)levelTimerCallback:(NSTimer *)timer;
@end
微控制器
#import \"MicroController.h\"
@implementation MicroController
- (id)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,
nil];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
if ([recorder prepareToRecord] == YES){
[recorder record];
}else {
int errorCode = CFSwapInt32HostToBig ([error code]);
NSLog(@\"Error: %@ [%4.4s])\" , [error localizedDescription], (char*)&errorCode);
}
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
} else
NSLog([error description]);
// input \'level\' is in meter.mAveragePower
return self;
}
- (void)levelTimerCallback:(NSTimer *)timer {
[recorder updateMeters];
const double ALPHA = 1.0; // 0.05f
double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
if (lowPassResults > 0.55)
NSLog(@\"Mic blow detected\");
[recorder updateMeters];
NSLog(@\"Average input: %f Peak input: %f\", [recorder averagePowerForChannel:0], [recorder peakPowerForChannel:0]);
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@\"initiated\");
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,
nil];
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(levelTimerCallback:) userInfo: nil repeats: YES];
} else
NSLog([error description]);
}
- (void)dealloc {
[levelTimer release];
[recorder release];
[super dealloc];
}
@end
但是我无法使麦克风正常工作。我仅收到此输出,并且麦克风从不响应:
平均输入:-120.000000峰值输入:-120.000000
有什么错误的想法吗?
谢谢你的帮助 !
没有找到相关结果
已邀请:
4 个回复
曝匿弄罚
否则,完美编写的录音机将无法正常工作。就是这样..问题解决了! 此错误是一个难以检测的错误,希望此消息对处于同样困境的任何其他人有所帮助。
漂截嘘
只需添加代码
在上方作废\“-(无效)听{\” 谢谢。 再见
坝胺绣敝
和MicController.m
我的问题是:我需要将您发布的有关此问题的解决方案的代码放在哪里。 。 。我该如何解决这个问题?
皇小福另届