AVAudioRecorder - 最后裁剪/修剪录音
Posted
技术标签:
【中文标题】AVAudioRecorder - 最后裁剪/修剪录音【英文标题】:AVAudioRecorder - crops / trims voice recordings at the end 【发布时间】:2013-10-02 13:51:26 【问题描述】:我正在将应用程序中的单个单词语音录制到 .wav 文件中
我在录制内容时遇到了问题。例如,如果我记录自己说:“明天”,实际的 .wav 文件将记录“明天”或类似的内容。
我通过 http post 发送语音文件,以便听到服务器端录制的内容。我不知道如何轻松听到我在 iphone 上录制的内容。
以下是应用程序中的一些代码 sn-ps。
非常感谢您的帮助:)
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
@"MyAudioMemo.wav",
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
// Setup audio session
AVAudiosession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
// Define the recorder setting
NSDictionary *recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 8000.0],AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey,// kAudioFormatLinearPCM
[NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
[NSNumber numberWithInt: AVAudioQualityMedium],AVEncoderAudioQualityKey,nil];
// Initiate and prepare the recorder
NSError * error;
m_recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:&error];
if (error)
NSLog(@"Error init recorder%@", error.description);
[self setError:@"Failed to init recorder"];
m_recorder.delegate = self;
m_recorder.meteringEnabled = YES;
[m_recorder prepareToRecord];
....
- (void) startRecord
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
// Start recording
[m_recorder record];
[self.RecordPauseButton setTitle:@"Stop" forState:UIControlStateNormal];
- (void) stopRecord
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:NO error:nil];
// Stop Recording
[m_recorder stop];
[self.RecordPauseButton setTitle:@"Record" forState:UIControlStateNormal];
if (!self.ResultsViewController)
self.ResultsViewController = [[ResultsViewController alloc] initWithNibName:@"ResultsViewController" bundle:nil];
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(callProxyAndMoveScreen:)
userInfo:nil
repeats:NO];
[self.RecordPauseButton setEnabled:NO];
[self enterWaitState];
- (void) callProxyAndMoveScreen: (NSTimer *)timer
self.ResultsViewController.SearchResults = [tusearchProxy searchWithVoice:[m_recorder url]];
[self.navigationController pushViewController:self.ResultsViewController animated:YES];
[self exitWaitState];
【问题讨论】:
【参考方案1】:也许可以通过使用 AVAudioPlayer 来检查客户端录制是否正常。一个简单的方法是在录制完成时调用的委托方法中播放文件。
- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag;
这可能是一段代码来解决这个问题。
- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag
AVAudioPlayer *tempPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:avrecorder.url error:nil];
tempPlayer.numberOfLoops = 0;
[tempPlayer play];
如果事实证明它太快将其切断一秒,请在延迟 1 秒或任何情况下调用您的“stopRecord”函数。
希望对你有帮助
【讨论】:
以上是关于AVAudioRecorder - 最后裁剪/修剪录音的主要内容,如果未能解决你的问题,请参考以下文章