[英]IOS Mic detection for getting Voice fails first time IOS7
伙計們,我正在開發一個Ipad應用程序,在該應用程序中,我會播放3次嗶聲,然后檢測聲音。 但是問題在於,當我第一次進入閱讀屏幕並開始閱讀時,蜂鳴聲很好,但麥克風無法檢測到聲音。 如果移至另一個屏幕並再次進入閱讀屏幕,則蜂鳴聲非常完美,麥克風也可以正常工作。 發出嗶聲的代碼如下
- (void)playAudio
{
[self setupAudioSession];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[fileURL release];
self.click = newPlayer;
[newPlayer release];
[self.click setDelegate:self];
[self.click prepareToPlay];
[self.click play];
}
- (void)setupAudioSession {
static BOOL audioSessionSetup = NO;
if (audioSessionSetup) {
return;
}
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
audioSessionSetup = YES;
}
在發出嗶聲之后,我調用了此功能以檢測麥克風
-(void)startrecordingAfterCountdown{
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
[self.view setUserInteractionEnabled:true];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
//
// // static BOOL audioSessionSetup2 = NO;
// // if (audioSessionSetup2) {
// // //return;
// // }
// [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryRecord error: nil];
// UInt32 doSetProperty = 1;
//
// AudioSessionSetProperty kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
//
// [[AVAudioSession sharedInstance] setActive: YES error: nil];
//
// // audioSessionSetup2 = YES;
// AVAudioSession *audioSession = [AVAudioSession sharedInstance];
// [audioSession setActive:YES error:nil];
// [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
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%@",[error description]);
}
- (void)levelTimerCallback:(NSTimer *)timer {
[recorder updateMeters];
const double ALPHA = 0.05;
double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
if (lowPassResults > 0.20){
NSLog(@"%2f",lowPassResults);
}
}
請告訴我我該如何解決..該麥克風將在第一時間檢測到語音
為了捕獲音頻,在調用prepareToRecord
之前,您需要在代碼中prepareToRecord
:
NSError *error;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&error];
進行呼叫后,請確保檢查error
對象,以防設置類別時出錯。
大家好,我只是通過更改代碼來解決此問題。 -(void)setupaudioSession && -(void)startrecordingAfterCountdown methods.
- (void)setupAudioSession {
static BOOL audioSessionSetup = NO;
if (audioSessionSetup) {
// return;
}
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
audioSessionSetup = YES;
}
-(void)startrecordingAfterCountdown{
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
[self.view setUserInteractionEnabled:true];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
// static BOOL audioSessionSetup2 = NO;
// if (audioSessionSetup2) {
// //return;
// }
// [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryRecord error: nil];
// UInt32 doSetProperty = 1;
//AudioSessionSetProperty kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
// [[AVAudioSession sharedInstance] setActive: YES error: nil];
// audioSessionSetup2 = YES;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:YES error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
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%@",[error description]);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.