简体   繁体   中英

iOS - Recording Audio and Playing Back

I'm trying to write an app that will allow me to record a message, then have the user playback what they've record, but for some reason this code isn't working. When the playAction method is invoked, I hear nothing but dead air. Any Ideas?

- (void) playAction
{
    NSLog(@"PlaY action");

    NSError *error;
    AVAudioPlayer *player = [[AVAudioPlayer alloc]
                                  initWithContentsOfURL:[NSURL fileURLWithPath:recorderFilePath] error:&error];
//    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:self.audioData error:&error];
    [player prepareToPlay];
    [player play];

}
- (void) startRecording
{
    [self.timer fire];
    self.recordButton.userInteractionEnabled = NO;
    self.stopButton.hidden = NO;
    self.playButton.hidden = YES;

    self.counterLabel.hidden = NO;
    self.viewTitle.hidden = YES;
    self.shortBlurb.hidden = YES;

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *err = nil;
    [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
    if(err){
        NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        return;
    }
    [audioSession setActive:YES error:&err];
    err = nil;
    if(err){
        NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        return;
    }

    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

    [recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    [recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];

    // Create a new dated file
    NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
    NSString *caldate = [now description];
    recorderFilePath = [NSString stringWithFormat:@"%@/%@.caf", DOCUMENTS_FOLDER, caldate];

    NSURL *url = [NSURL fileURLWithPath:recorderFilePath];
    err = nil;
    recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
    if(!recorder){
        NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        UIAlertView *alert =
        [[UIAlertView alloc] initWithTitle: @"Warning"
                                   message: [err localizedDescription]
                                  delegate: nil
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
        [alert show];
        return;
    }

    //prepare to record
    [recorder setDelegate:self];
    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;

    BOOL audioHWAvailable = audioSession.inputAvailable;
    if (! audioHWAvailable) {
        UIAlertView *cantRecordAlert =
        [[UIAlertView alloc] initWithTitle: @"Warning"
                                   message: @"Audio input hardware not available"
                                  delegate: nil
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
        [cantRecordAlert show];
        return;
    }

    // start recording
    [recorder recordForDuration:(NSTimeInterval) 7];
}

- (void) stopRecording{

    self.counterLabel.hidden = YES;
    self.recordButton.userInteractionEnabled = YES;

    self.viewTitle.hidden = NO;
    self.shortBlurb.hidden = NO;

    self.recordButton.hidden = NO;
    self.playButton.hidden = NO;
    self.stopButton.hidden = YES;

    [recorder stop];

    NSURL *url = [NSURL fileURLWithPath: recorderFilePath];
    NSError *err = nil;
    self.audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
    if(!audioData)
    {
        NSLog(@"audio data: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    }
    //[editedObject setValue:[NSData dataWithContentsOfURL:url] forKey:editedFieldKey];

    //[recorder deleteRecording];


    NSFileManager *fm = [NSFileManager defaultManager];

    err = nil;
    //[fm removeItemAtPath:[url path] error:&err];
    if(err)
        NSLog(@"File Manager: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
}

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully:(BOOL)flag
{

    NSLog (@"audioRecorderDidFinishRecording:successfully:");
    // your actions here

}

It looks fine to me, except:

Try changing this:

AVAudioPlayer *player = [[AVAudioPlayer alloc]...

to:

@property(nonatomic, strong) AVAudioPlayer *player;

self.player = [[AVAudioPlayer alloc].....

Hope this 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