我需要使用AVAudioRecorder让用户每分钟心跳一次,并在我的应用程序中使用它。
我尝试使用AVAudioSession设置我的麦克风增益来获得I/P,但它无法获得心跳,有什么方法可以使用我的应用程序获得心跳?到目前为止我已经尝试过的代码
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//here i took cache directory as saved directory you can also take NSDocumentDirectory
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];//at this path your recorded audio will be saved
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc]initWithURL:soundFileURL settings:recordSettings error:&error];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
[audioSession setInputGain:1.0 error:nil];
if( !audioRecorder ) {
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: @"Warning" message: [error localizedDescription] delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return;
}
if ( error )
{
NSLog( @"error: %@", [error localizedDescription] );
}
else {
[audioRecorder prepareToRecord];
[self recordAudio];//Mehod to Record Audio
}}
-(void) recordAudio
{
if (!audioRecorder.recording)
{
audioRecorder.delegate = self;
[audioRecorder recordForDuration:10];//Here i took record duration as 10 secs
}
}发布于 2014-05-15 00:41:02
您可能想尝试使用峰值计功率技巧和以下教程中的低通滤波器代码。我想你的手机会在头顶的updateMeter上嗡嗡作响,以跟上你的心跳。基本逻辑是打开记录器,更新仪表,获得最大功率,运行滤波器,比较阈值以查看何时发生“节拍”,然后参考系统时间或使用重复计时器来运行上述功能。按手机麦克风裸露的胸部可能是最好的(没有辅助硬件),imho。祝好运!如果你能让它工作,请让我们知道。
http://mobileorchard.com/tutorial-detecting-when-a-user-blows-into-the-mic/
https://stackoverflow.com/questions/21660366
复制相似问题