ios音频录制和播放,文件很小
2015-10-06 09:20
483 查看
#pragma mark - 开始录音---test
- (void)beginRecordByFileName:(NSString*)_fileName
{
//设置文件名和录音路径
self.recordFileName = _fileName;
self.recordFilePath = [VoiceRecorderBaseVCgetPathByFileName:self.recordFileNameofType:@"wav"];
NSError *error = [[NSError alloc] init];
//初始化录音
self.recorder = [[[AVAudioRecorderalloc] initWithURL:[NSURLfileURLWithPath:self.recordFilePath] settings:[VoiceRecorderBaseVCgetAudioRecorderSettingDict] error:&error]autorelease];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorderprepareToRecord];
//开始录音
[[AVAudioSessionsharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecorderror:nil];
[[AVAudioSessionsharedInstance] setActive:YESerror:nil];
[recorder record];
}
#pragma mark - wavToAmr转换
- (IBAction)wav2Amr:(id)sender {
if (_originWav.length > 0){
self.convertAmr = [_originWavstringByAppendingString:@"wavToAmr"];
//转格式
NSFileManager* fm=[NSFileManagerdefaultManager];
NSData *wavdata = [fm contentsAtPath:[VoiceRecorderBaseVC getPathByFileName:_originWavofType:@"wav"]];
NSString * wav = [NSString stringWithFormat:@"wavLength = %lu",(unsigned long)wavdata.length];
self.wavLabel.text = wav;
[VoiceConverterwavToAmr:[VoiceRecorderBaseVCgetPathByFileName:_originWavofType:@"wav"]amrSavePath:[VoiceRecorderBaseVCgetPathByFileName:_convertAmrofType:@"amr"]];
//发送音频数据 因为要播放无压缩音频,因此音频名字上传是原wav名,但上传的data数据是转换后的arm
NSData* data = [NSDatadataWithContentsOfFile:[VoiceRecorderBaseVCgetPathByFileName:_convertAmrofType:@"amr"]];
NSLog(@"amrlength = %lu",(unsigned long)data.length);
NSString * amr = [NSString stringWithFormat:@"amrlength = %lu",(unsigned long)data.length];
self.amrLabel.text = amr;
}
}
#pragma mark - 开始录音
- (IBAction)startButtonAction:(id)sender {
startdate = [NSDate date];
self.timeLabel.text = [NSStringstringWithFormat:@"%@",startdate];
//设置文件名
self.originWav = [VoiceRecorderBaseVCgetCurrentTimeString];
//开始录音
[selfbeginRecordByFileName:self.originWav];
}
#pragma mark - 停止录音
- (IBAction)stopButtonAction:(id)sender {
stopdate = [NSD
a9c5
ate date];
self.stopTimeLabel.text = [NSStringstringWithFormat:@"%@",stopdate];
//停止录音
if (recorder.isRecording){
[recorder stop];
}
//回调录音文件路径
if ([self.vrbDelegate respondsToSelector:@selector(VoiceRecorderBaseVCRecordFinish:fileName:)]){
[self.vrbDelegateVoiceRecorderBaseVCRecordFinish:recordFilePathfileName:recordFileName];
}
}
#pragma mark - 播放录音
- (IBAction)playVoiceButtonAction:(id)sender {
if ([[NSFileManagerdefaultManager] fileExistsAtPath:[VoiceRecorderBaseVCgetPathByFileName:_originWavofType:@"wav"]]) {
player = [playerinitWithContentsOfURL:[NSURLfileURLWithPath:[VoiceRecorderBaseVCgetPathByFileName:_originWavofType:@"wav"]] error:nil];
if (![player isPlaying]) {
player.delegate = self;
[player play];
}
}
}
- (void)beginRecordByFileName:(NSString*)_fileName
{
//设置文件名和录音路径
self.recordFileName = _fileName;
self.recordFilePath = [VoiceRecorderBaseVCgetPathByFileName:self.recordFileNameofType:@"wav"];
NSError *error = [[NSError alloc] init];
//初始化录音
self.recorder = [[[AVAudioRecorderalloc] initWithURL:[NSURLfileURLWithPath:self.recordFilePath] settings:[VoiceRecorderBaseVCgetAudioRecorderSettingDict] error:&error]autorelease];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorderprepareToRecord];
//开始录音
[[AVAudioSessionsharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecorderror:nil];
[[AVAudioSessionsharedInstance] setActive:YESerror:nil];
[recorder record];
}
#pragma mark - wavToAmr转换
- (IBAction)wav2Amr:(id)sender {
if (_originWav.length > 0){
self.convertAmr = [_originWavstringByAppendingString:@"wavToAmr"];
//转格式
NSFileManager* fm=[NSFileManagerdefaultManager];
NSData *wavdata = [fm contentsAtPath:[VoiceRecorderBaseVC getPathByFileName:_originWavofType:@"wav"]];
NSString * wav = [NSString stringWithFormat:@"wavLength = %lu",(unsigned long)wavdata.length];
self.wavLabel.text = wav;
[VoiceConverterwavToAmr:[VoiceRecorderBaseVCgetPathByFileName:_originWavofType:@"wav"]amrSavePath:[VoiceRecorderBaseVCgetPathByFileName:_convertAmrofType:@"amr"]];
//发送音频数据 因为要播放无压缩音频,因此音频名字上传是原wav名,但上传的data数据是转换后的arm
NSData* data = [NSDatadataWithContentsOfFile:[VoiceRecorderBaseVCgetPathByFileName:_convertAmrofType:@"amr"]];
NSLog(@"amrlength = %lu",(unsigned long)data.length);
NSString * amr = [NSString stringWithFormat:@"amrlength = %lu",(unsigned long)data.length];
self.amrLabel.text = amr;
}
}
#pragma mark - 开始录音
- (IBAction)startButtonAction:(id)sender {
startdate = [NSDate date];
self.timeLabel.text = [NSStringstringWithFormat:@"%@",startdate];
//设置文件名
self.originWav = [VoiceRecorderBaseVCgetCurrentTimeString];
//开始录音
[selfbeginRecordByFileName:self.originWav];
}
#pragma mark - 停止录音
- (IBAction)stopButtonAction:(id)sender {
stopdate = [NSD
a9c5
ate date];
self.stopTimeLabel.text = [NSStringstringWithFormat:@"%@",stopdate];
//停止录音
if (recorder.isRecording){
[recorder stop];
}
//回调录音文件路径
if ([self.vrbDelegate respondsToSelector:@selector(VoiceRecorderBaseVCRecordFinish:fileName:)]){
[self.vrbDelegateVoiceRecorderBaseVCRecordFinish:recordFilePathfileName:recordFileName];
}
}
#pragma mark - 播放录音
- (IBAction)playVoiceButtonAction:(id)sender {
if ([[NSFileManagerdefaultManager] fileExistsAtPath:[VoiceRecorderBaseVCgetPathByFileName:_originWavofType:@"wav"]]) {
player = [playerinitWithContentsOfURL:[NSURLfileURLWithPath:[VoiceRecorderBaseVCgetPathByFileName:_originWavofType:@"wav"]] error:nil];
if (![player isPlaying]) {
player.delegate = self;
[player play];
}
}
}
相关文章推荐
- iOS 支付功能
- iOS即时通讯 Socket
- iOS runloop讲解
- iOS runtime讲解,并且用runtime动态归档与解档
- iOS 高德地图
- iOS 百度地图
- iOS block传值
- iOS coredata的使用及版本升级
- iOS M3U8视频的下载与播放
- iOS masonry进行页面适配
- iOS AFN进行断点下载
- iOS FMDB自己封装的单例类
- 【IOS 开发学习总结-OC-32】★OC之foundation 框架——谓词(NSPredicate)
- ios:kvo概述
- iOS 新浪微博-2.0 搜索框/标题带箭头/下拉菜单
- ios-发送邮件两种方式实现
- ios-发送信息的两种方式
- iOS开发小技巧之快捷键的使用--极少用(甚至不用)鼠标开发iOS程序
- iOS AVPlayer播放模式的实现(随机播放 列表循环 单曲循环)
- iOS XIB以及Storyboard的部分使用方法