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

本文介绍了一个简单的iOS应用中实现录音和播放功能的方法。通过使用AVFoundation框架中的AVAudioRecorder和AVAudioPlayer类来完成录音文件的创建、保存及播放。文章详细展示了如何设置录音参数、初始化录音器以及控制录音状态等关键步骤。
摘要由CSDN通过智能技术生成
-(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];
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值