2013-10-02 33 views
2

修剪录音,我录制语音单个词在我的应用程序,以.wav文件AVAudioRecorder - 作物/末

我现在遇到一个问题,当我记录一些东西。例如,如果我记录自己说:“明天”,实际的.wav文件将记录“明天”或类似的东西。

我通过http发送语音文件发布,所以我听到什么是记录在服务器端。我不知道如何轻松听到我在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]; 
} 

回答

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

如果事实证明,它正在削减其关闭第二太快了,打电话给你“stopRecord”功能,1秒,或任何情况而定的延迟。

希望它有帮助