title : iOS 录音-上传与播放解析
category : IOS
iOS 录音-上传与播放解析
标签(空格分隔): IOS
一 iOS本地录音
- 准备工作
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];
//设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
//设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
[recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
//录音通道数 1 或 2
[recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
//线性采样位数 8、16、24、32
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//录音的质量
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/aaa"];
NSURL* url = [NSURL fileURLWithPath:path];
_recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:nil];
//准备录音
[_recorder prepareToRecord];
- 加上点击事件之后(可设置按钮,可添加手势)
[_recorder record];
- 点击事件结束之后记得,停止录音
[_recorder record];
- 将录音文件写入目录中,方便下次播放
NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/aaa"];
NSURL* url = [NSURL fileURLWithPath:path];
NSData *data = [NSData dataWithContentsOfURL:url];
[data writeToFile:[NSTemporaryDirectory() stringByAppendingPathComponent:@"tmp/aaa"] atomically:YES];
到此,录音结束,但注意,如果要上传录音文件,因为iOS默认录音格式不支持amr格式音频文件。所以,此时上传的是wav格式的MP3文件,文件比较大。
一般来说,上传的时候,我们会将音频文件转成amr格式的音乐文件,这样,既支持安卓默认播放格式,文件也会小很多,服务端处理也方便。
后面会介绍如何转换。
二 播放本地音频文件
- 之前录音写入了文件中,且地址是
NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/aaa"];
NSURL* url = [NSURL fileURLWithPath:path];
- 创建播放器,并播放
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[_player prepareToPlay];
[_player play];
三 本地录音WAV转AMR
- 首先我们需要使用一个三方库:VoiceConvert
使用VoiceConvert 录音:
NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"download/aaa"];
[[EMCDDeviceManager sharedInstance] asyncStartRecordingWithFileName:path completion:^(NSError *error)
{
if (error) {
NSLog(@"%@",NSEaseLocalizedString(@"message.startRecordFail", @"failure to start recording"));
}
}];
注意这里录制的音频文件可是是 AMR格式,可直接上传了。
使用VoiceConvert 停止录音:
[[EMCDDeviceManager sharedInstance] asyncStopRecordingWithCompletion:^(NSString *recordPath, NSInteger aDuration, NSError *error) {
if (!error) {
//获取到的录音地址,自己存起来。也可以写入FileManage中,这里我写入了沙盒中。
dispatch_async(dispatch_get_main_queue(), ^{
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setObject:recordPath forKey:@"music"];
[user setObject:[NSNumber numberWithInteger:aDuration] forKey:@"musicTime"];
});
}
}];
使用VoiceConvert 播放录音:
[[EMCDDeviceManager sharedInstance] asyncPlayingWithPath:model.fileLocalPath completion:^(NSError *error) {
//这里做一些播放结束的其他操作
[[EaseMessageReadManager defaultManager] stopMessageAudioModel];
dispatch_async(dispatch_get_main_queue(), ^{
[[EMCDDeviceManager sharedInstance] disableProximitySensor];
});
}];
使用VoiceConvert 播放网络下载的AMR录音:
先将AMR转成WAV格式,因为iOS 不支持播放AMR格式音频文件。
//获取链接数据转成Data
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[IimageUrlPre stringByAppendingString:[cur user_info_mp3]]]];
//把data写入文件中,取名AudioTempFile
[data writeToFile:[NSTemporaryDirectory() stringByAppendingPathComponent:@"AudioTempFile"] atomically:YES];
//将数据amr格式的Data转成wav
[EMVoiceConverter amrToWav:[NSTemporaryDirectory() stringByAppendingPathComponent:@"AudioTempFile"] wavSavePath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"AudioTempConvertFile"]];
//读取新的wav格式音频文件
NSData* convertData = [NSData dataWithContentsOfFile:[NSTemporaryDirectory() stringByAppendingPathComponent:@"AudioTempConvertFile"]];
//初始化播放器,播放converData
self.player = [[AVAudioPlayer alloc] initWithData:convertData error:NULL];
[self.player prepareToPlay];
[self.player play];
网友评论
clang: error: linker command failed with exit code 1 (use -v to see invocation) 运行报错这个怎么回事