首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iOS中使用AVAudioRecorder获取用户的HeartBeat

在iOS中使用AVAudioRecorder获取用户的HeartBeat
EN

Stack Overflow用户
提问于 2014-02-09 22:34:49
回答 1查看 439关注 0票数 0

我需要使用AVAudioRecorder让用户每分钟心跳一次,并在我的应用程序中使用它。

我尝试使用AVAudioSession设置我的麦克风增益来获得I/P,但它无法获得心跳,有什么方法可以使用我的应用程序获得心跳?到目前为止我已经尝试过的代码

代码语言:javascript
复制
 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
}

}

代码语言:javascript
复制
-(void) recordAudio
{
 if (!audioRecorder.recording)
  {
   audioRecorder.delegate = self;
  [audioRecorder recordForDuration:10];//Here i took record duration as 10 secs 

 }
 }
EN

回答 1

Stack Overflow用户

发布于 2014-05-15 00:41:02

您可能想尝试使用峰值计功率技巧和以下教程中的低通滤波器代码。我想你的手机会在头顶的updateMeter上嗡嗡作响,以跟上你的心跳。基本逻辑是打开记录器,更新仪表,获得最大功率,运行滤波器,比较阈值以查看何时发生“节拍”,然后参考系统时间或使用重复计时器来运行上述功能。按手机麦克风裸露的胸部可能是最好的(没有辅助硬件),imho。祝好运!如果你能让它工作,请让我们知道。

http://mobileorchard.com/tutorial-detecting-when-a-user-blows-into-the-mic/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21660366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档