iOS 录音转MP3格式
参考:https://github.com/molon/MLAudioRecorder 非常感谢
可以借助demo进行学习,demo地址: https://github.com/qiweiqiang10/iOS-MP3.git
.h 文件
p.p1 { margin: 0; font: 20px Menlo; color: rgba(200, 27, 19, 1); background-color: rgba(255, 255, 255, 1) }
p.p2 { margin: 0; font: 12px Helvetica; background-color: rgba(255, 255, 255, 1); min-height: 14px }
p.p3 { margin: 0; font: 20px Menlo; color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1) }
p.p4 { margin: 0; font: 20px Menlo; color: rgba(196, 34, 117, 1); background-color: rgba(255, 255, 255, 1) }
span.s1 { color: rgba(130, 45, 15, 1) }
span.s2 { color: rgba(196, 34, 117, 1) }
span.s3 { color: rgba(97, 34, 174, 1) }
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
– (IBAction)didClickRecordButon:(id)sender;
– (IBAction)didClickPlaybutton:(id)sender;
@end
.m文件
p.p1 { margin: 0; font: 20px Menlo; color: rgba(200, 27, 19, 1); background-color: rgba(255, 255, 255, 1) }
p.p2 { margin: 0; font: 12px Helvetica; background-color: rgba(255, 255, 255, 1); min-height: 14px }
p.p3 { margin: 0; font: 20px Menlo; color: rgba(97, 34, 174, 1); background-color: rgba(255, 255, 255, 1) }
p.p4 { margin: 0; font: 20px Menlo; color: rgba(196, 34, 117, 1); background-color: rgba(255, 255, 255, 1) }
p.p5 { margin: 0; font: 20px Menlo; color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1) }
p.p6 { margin: 0; font: 20px Menlo; color: rgba(62, 30, 129, 1); background-color: rgba(255, 255, 255, 1) }
p.p7 { margin: 0; font: 20px Menlo; color: rgba(60, 130, 139, 1); background-color: rgba(255, 255, 255, 1) }
p.p8 { margin: 0; font: 20px Menlo; color: rgba(30, 148, 33, 1); background-color: rgba(255, 255, 255, 1) }
p.p9 { margin: 0; font: 20px Menlo; color: rgba(83, 154, 164, 1); background-color: rgba(255, 255, 255, 1) }
p.p10 { margin: 0; font: 20px Menlo; color: rgba(41, 76, 80, 1); background-color: rgba(255, 255, 255, 1) }
p.p11 { margin: 0; font: 20px Menlo; color: rgba(112, 61, 170, 1); background-color: rgba(255, 255, 255, 1) }
span.s1 { color: rgba(130, 45, 15, 1) }
span.s2 { color: rgba(196, 34, 117, 1) }
span.s3 { color: rgba(0, 0, 0, 1) }
span.s4 { color: rgba(60, 130, 139, 1) }
span.s5 { color: rgba(97, 34, 174, 1) }
span.s6 { color: rgba(4, 53, 255, 1) }
span.s7 { color: rgba(62, 30, 129, 1) }
span.s8 { color: rgba(83, 154, 164, 1) }
span.s9 { color: rgba(200, 27, 19, 1) }
span.s10 { color: rgba(120, 73, 42, 1) }
span.s11 { color: rgba(41, 76, 80, 1) }
span.s12 { color: rgba(112, 61, 170, 1) }
#import “ViewController.h”
#import <AVFoundation/AVFoundation.h>
#import “MLAudioRecorder.h”
#import “CafRecordWriter.h”
#import “AmrRecordWriter.h”
#import “Mp3RecordWriter.h”
#import “MLAudioMeterObserver.h”
@interface ViewController ()<AVAudioPlayerDelegate,AVAudioRecorderDelegate>
@property (nonatomic, strong) MLAudioRecorder *recorder;
@property (nonatomic, strong) CafRecordWriter *cafWriter;
@property (nonatomic, strong) AmrRecordWriter *amrWriter;
@property (nonatomic, strong) Mp3RecordWriter *mp3Writer;
@property (nonatomic, strong) MLAudioMeterObserver *meterObserver;
@property (retain, nonatomic) AVAudioPlayer *audioPlayer;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
CafRecordWriter *writer = [[CafRecordWriter alloc]init];
writer.filePath = [path stringByAppendingPathComponent:@”record.caf”];
self.cafWriter = writer;
AmrRecordWriter *amrWriter = [[AmrRecordWriter alloc]init];
amrWriter.filePath = [path stringByAppendingPathComponent:@”record.amr”];
amrWriter.maxSecondCount = 60;
amrWriter.maxFileSize = 1024*256;
self.amrWriter = amrWriter;
Mp3RecordWriter *mp3Writer = [[Mp3RecordWriter alloc]init];
mp3Writer.filePath = [path stringByAppendingPathComponent:@”record.mp3″];
// mp3Writer.maxSecondCount = 60;
// mp3Writer.maxFileSize = 1024*256;
mp3Writer.maxSecondCount = 300;
mp3Writer.maxFileSize = 1024*256 * 100;
self.mp3Writer = mp3Writer;
MLAudioMeterObserver *meterObserver = [[MLAudioMeterObserver alloc]init];
meterObserver.actionBlock = ^(NSArray *levelMeterStates,MLAudioMeterObserver *meterObserver){
DLOG(@”volume:%f”,[MLAudioMeterObserver volumeForLevelMeterStates:levelMeterStates]);
};
meterObserver.errorBlock = ^(NSError *error,MLAudioMeterObserver *meterObserver){
[[[UIAlertView alloc]initWithTitle:@”错误” message:error.userInfo[NSLocalizedDescriptionKey] delegate:nil cancelButtonTitle:nil otherButtonTitles:@”知道了”, nil]show];
};
self.meterObserver = meterObserver;
MLAudioRecorder *recorder = [[MLAudioRecorder alloc]init];
__weak __typeof(self)weakSelf = self;
recorder.receiveStoppedBlock = ^{
// [weakSelf.recordButton setTitle:@”Record” forState:UIControlStateNormal];
weakSelf.meterObserver.audioQueue = nil;
};
recorder.receiveErrorBlock = ^(NSError *error){
// [weakSelf.recordButton setTitle:@”Record” forState:UIControlStateNormal];
weakSelf.meterObserver.audioQueue = nil;
[[[UIAlertView alloc]initWithTitle:@”错误” message:error.userInfo[NSLocalizedDescriptionKey] delegate:nil cancelButtonTitle:nil otherButtonTitles:@”知道了”, nil]show];
};
//caf
// recorder.fileWriterDelegate = writer;
// self.filePath = writer.filePath;
//mp3
recorder.fileWriterDelegate = mp3Writer;
// self.filePath = mp3Writer.filePath;
//amr
// recorder.bufferDurationSeconds = 0.25;
// recorder.fileWriterDelegate = self.amrWriter;
self.recorder = recorder;
}
– (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
– (IBAction)didClickRecordButon:(id)sender {
[[AVAudioSession sharedInstance]requestRecordPermission:^(BOOL granted) {
if (granted) {
NSLog(@”开始录制”);
[self.recorder startRecording];
self.meterObserver.audioQueue = self.recorder->_audioQueue;
}
}];
}
– (IBAction)didClickPlaybutton:(id)sender {
[self.recorder stopRecording];
NSLog(@”%@——-%@”,_amrWriter.filePath,_mp3Writer.filePath);
}
– (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
if (_audioPlayer.isPlaying) {
[_audioPlayer stop];
}
long long fileLength = [self fileSizeAtPath:_mp3Writer.filePath];
NSLog(@”%lld”,fileLength);
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:self.mp3Writer.filePath] error:nil];
_audioPlayer.delegate = self;
[_audioPlayer play];
}
– (long long) fileSizeAtPath:(NSString*) filePath{
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;
}