4

我正在将应用程序中的单个单词语音录制到 .wav 文件中

录制内容时遇到问题。例如,如果我记录自己说:“明天”,实际的 .wav 文件将记录“明天”或类似的内容。

我通过 http post 发送语音文件,以便听到服务器端录制的内容。我不知道如何轻松地听到我在 iphone 上录制的内容。

以下是应用程序的一些代码片段。

我将衷心感谢您的帮助 :)

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];
}
4

1 回答 1

0

也许通过使用 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”函数。

希望能帮助到你

于 2013-12-06T13:36:28.553 回答