欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-11 20:14:47
...
-(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/iPad 开发:录音及声音播放
            
    
    博客分类: iPhone/iPad开发  
  • 大小: 95.5 KB