iOS上录音和播放声音 大概有几种方式
1 使用AVAudioRecorder录音,使用AVAudioPlayer来播放声音。
2 使用AudioQueueRef来录音和播放声音,这个API比较麻烦,要维护缓冲区之类的挺折腾。
3 使用AVPlayer来播放,这个吃入的文件格式比较多(多种音、视频文件) 在文档库里有一个官方的Demo叫AVPlayerDemo。可以学习他的用法。
下面主要说一下第一种方式。
录音
录音之前,先调用下面的语句,询问用户是否可以访问他的 隐私设备麦克风
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted)
{}]得到允许之后,开始准备录音机录音
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: AVSampleRateValue], AVSampleRateKey,
[NSNumber numberWithInt: AVFormatIDValue], AVFormatIDKey,
[NSNumber numberWithInt: AVNumberOfChannelsValue], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVEncoderAudioQualityValue], AVEncoderAudioQualityKey,
nil];[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
error:nil];
AVAudioRecorder* _recorder = [[AVAudioRecorder alloc] initWithURL:strMyCacheFilePathName settings:settings error:&error];
[_recorder record];//开始录音录音后的文件存储在strMyCacheFilePathName文件里,是一个原始的PCM文件,未经编码压缩等步骤的原始文件,体积比较大。
播放也比较简单
AVAudioPlayer* _mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:strMyCacheFilePathName
error:&playerError];
_mp3Player.delegate = self;
[_mp3Player play];好了,简单的录音,播放就做完了。
本文详细介绍了在iOS平台上使用AVAudioRecorder进行录音及AVAudioPlayer进行播放的方法,包括设置音频参数、请求麦克风权限、初始化录音机与播放器等关键步骤。重点突出录音文件的原始保存方式和播放过程的简化。
2868

被折叠的 条评论
为什么被折叠?



