简体   繁体   中英

AVAudioRecorder - crops / trims voice recordings at the end

I'm recording single words in voice in my application to a .wav file

I'm encountering an issue when I record something. For example if I record myself saying: "tomorrow" the actual .wav file will record "tomor" or something like that.

I send the voice file over http post so I hear what was recorded on the server side. I don't know how to easily hear what I recorded on the iphone.

Below are some code snippets from the application.

I would really appreciate your help :)

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

Perhaps to check if the client side recording is fine by using an AVAudioPlayer. An easy way to do this is just playback the file in the delegate method thats called when recording has finished.

- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag;

This is a possible bit of code to do the trick.

 - (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag{
    AVAudioPlayer *tempPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:avrecorder.url error:nil];
    tempPlayer.numberOfLoops = 0;
    [tempPlayer play];
}

If it turns out that it is cutting it off a second too fast, call your "stopRecord" function with a delay of 1 second, or whatever the case may be.

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM