![](/img/trans.png)
[英]IOS AVAudioRecorder, How to record only when audio input present (non-silence)
[英]iOS AVAudioRecorder- how to record silence while user is on a phone call
這是我現在擁有的代碼,用於在用戶接到電話時暫停錄音並在他們掛斷電話后恢復錄音。
- (void)handleAudioSessionInterruption:(NSNotification *)notification
{
NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
switch (interruptionType.unsignedIntegerValue) {
case AVAudioSessionInterruptionTypeBegan:{
_currentAudioSessionMode = EXAVAudioSessionModeInactive;
if (_audioRecorder.recording) {
[_audioRecorder pause];
[self demoteAudioSessionIfPossible];
}
} break;
case AVAudioSessionInterruptionTypeEnded:{
_currentAudioSessionMode = EXAVAudioSessionModeActive;
if (_audioRecorder && !_audioRecorder.recording) {
if (_allowsAudioRecording) {
[self promoteAudioSessionIfNecessary];
_audioRecorderShouldBeginRecording = true;
[_audioRecorder record];
}
_audioRecorderShouldBeginRecording = false;
}
} break;
default:
break;
}
我想要實現的是繼續錄音,但在用戶打電話時捕捉純粹的沉默。 這就是它在 Android 上的工作方式,我們關心錄音的正確持續時間。 知道怎么做嗎?
#import <AVFoundation/AVFoundation.h>
設置音頻 session 使用 AVAudioSessionCategoryPlayAndRecord 類別並激活它:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
使用 AVAudioRecorder class 設置錄音機,指定將保存音頻文件的 URL 和錄音的音頻設置:
NSURL *url = [NSURL fileURLWithPath:@"/path/to/audio/file.m4a"];
NSDictionary *settings = @{
AVFormatIDKey: @(kAudioFormatMPEG4AAC),
AVSampleRateKey: @44100.0f,
AVNumberOfChannelsKey: @1,
};
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:nil];
通過調用錄音機上的 record 方法開始錄音:
[recorder record];
要停止錄音,請調用錄音機的 stop 方法:
[recorder stop];
要在用戶通話時錄制靜音,您可以使用 audioRecorderDidFinishRecording:successfully: 委托方法來檢測錄音何時因通話而停止並開始新的錄音。
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {
// Check if the recording was stopped due to a phone call
if (!flag) {
// Start a new recording
[recorder record];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.