iPhone/iPad 开发:录音及声音播放

-(IBAction) recordOrStop: (id) sender {
if (recording) {
        [soundRecorder stop];
        recording = NO;
        self.soundRecorder = nil;

        [recordOrStopButton setTitle: @"Record" forState: UIControlStateNormal];
        [recordOrStopButton setTitle: @"Record" forState: UIControlStateHighlighted];

        [[AVAudioSession sharedInstance] setActive: NO error: nil];
btnPlay.enabled = YES;

    } else {

        [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];

        NSDictionary *recordSettings =
[[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
nil];

        AVAudioRecorder *newRecorder = [[AVAudioRecorder alloc] initWithURL: soundFileURL
                                                                   settings: recordSettings
                                                                      error: &error];
        [recordSettings release];
        self.soundRecorder = newRecorder;
        [newRecorder release];

        soundRecorder.delegate = self;
        [soundRecorder prepareToRecord];
        [soundRecorder record];
        [recordOrStopButton setTitle: @"Stop" forState: UIControlStateNormal];
        [recordOrStopButton setTitle: @"Stop" forState: UIControlStateHighlighted];

        recording = YES;
//btnPlay.enabled = NO;
    }
}

-(IBAction) playVoice: (id) sender{
NSError *err;
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&err];
theAudio.delegate=self;
if (theAudio==nil) {
NSLog(@"音声放送:%@",[err description]);
}
else {
theAudio.volume=1.0f;
[theAudio prepareToPlay];
[theAudio play];
}
}

你可能感兴趣的:(iPhone)