代理
AVAudioPlayerDelegate,AVAudioRecorderDelegate
{
UIButton *voiceBtn;
}
@property (nonatomic,strong)AVAudioRecorder *audioRecorder;//音频录音机
@property (nonatomic,strong)AVAudioPlayer *audioPlayer;
- (void)viewDidLoad {
[selfsetAudioSession];
}
#pragma mark - 私有方法
/**
* 设置音频会话
*/
-(void)setAudioSession{
AVAudioSession *audioSession=[AVAudioSessionsharedInstance];
//设置为播放和录音状态,以便可以在录制完之后播放录音
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];
[audioSession setActive:YESerror:nil];
//设置录音声音
NSError *audioError =nil;
BOOL success = [audioSessionoverrideOutputAudioPort:AVAudioSessionPortOverrideSpeakererror:&audioError];
if(!success){
NSLog(@"error doing outputaudioportoverride - %@", [audioErrorlocalizedDescription]);
}
}
/**
* 取得录音文件保存路径
*
* @return 录音文件路径
*/
-(NSURL *)getSavePath{
NSString *urlStr=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject];
urlStr=[urlStr stringByAppendingPathComponent:@"record.caf"];
NSLog(@"file path:%@",urlStr);
NSURL *url=[NSURLfileURLWithPath:urlStr];
return url;
}
/**
* 取得录音文件设置
*
* @return 录音设置
*/
-(NSDictionary *)getAudioSetting{
NSDictionary *recordFileSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumbernumberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumbernumberWithInt:16],
AVEncoderBitRateKey,
[NSNumbernumberWithInt:2],
AVNumberOfChannelsKey,
[NSNumbernumberWithFloat:44100.0],
AVSampleRateKey,
nil];
return recordFileSettings;
}
/**
* 获得录音机对象
*
* @return 录音机对象
*/
-(AVAudioRecorder *)audioRecorder{
if (!_audioRecorder) {
//创建录音文件保存路径
NSURL *url=[selfgetSavePath];
//创建录音格式设置
NSDictionary *setting=[selfgetAudioSetting];
//创建录音机
NSError *error=nil;
_audioRecorder=[[AVAudioRecorderalloc]initWithURL:urlsettings:settingerror:&error];
_audioRecorder.delegate=self;
_audioRecorder.meteringEnabled=YES;//如果要监控声波则必须设置为YES
if (error) {
NSLog(@"创建录音机对象时发生错误,错误信息:%@",error.localizedDescription);
returnnil;
}
}
return_audioRecorder;
}
/**
* 创建播放器
*
* @return 播放器
*/
-(AVAudioPlayer *)audioPlayer{
if (!_audioPlayer) {
NSURL *url=[selfgetSavePath];
NSError *error=nil;
_audioPlayer=[[AVAudioPlayeralloc]initWithContentsOfURL:urlerror:&error];
_audioPlayer.numberOfLoops=0;
[_audioPlayerprepareToPlay];
if (error) {
NSLog(@"创建播放器过程中发生错误,错误信息:%@",error.localizedDescription);
returnnil;
}
}
return_audioPlayer;
}
#pragma mark - 录音机代理方法
/**
* 录音完成,录音完成后播放录音
*
* @param recorder 录音机对象
* @param flag 是否成功
*/
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
if (![self.audioPlayerisPlaying]) {
[selftoMp3];
}
NSLog(@"录音完成!");
}
- (void)toMp3
{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject];
NSString *cafFilePath = [pathstringByAppendingPathComponent:@"record.caf"];
NSString *mp3FilePath = [pathstringByAppendingPathComponent:@"record.mp3"];
@try {
int read, write;
FILE *pcm =fopen([cafFilePathcStringUsingEncoding:1],"rb");//被转换的文件
FILE *mp3 =fopen([mp3FilePathcStringUsingEncoding:1],"wb");//转换后文件的存放位置
constint PCM_SIZE =8192;
constint MP3_SIZE =8192;
shortint pcm_buffer[PCM_SIZE*2];
unsignedchar mp3_buffer[MP3_SIZE];
lame_t lame =lame_init();
lame_set_in_samplerate(lame,44100);
lame_set_VBR(lame,vbr_default);
lame_init_params(lame);
do {
read = fread(pcm_buffer,2*sizeof(shortint), PCM_SIZE, pcm);
if (read ==0)
write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
else
write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
fwrite(mp3_buffer, write,1, mp3);
} while (read !=0);
lame_close(lame);
fclose(mp3);
fclose(pcm);
}
@catch (NSException *exception) {
// NSLog(@"%@",[exception description]);
}
}
//开始录音
- (void)onClickVoice{
if (voiceBtn.tag ==5) {
voiceBtn.tag =6;
if (![self.audioRecorderisRecording]) {
[self.audioRecorderrecord];//首次使用应用时如果调用record方法会询问用户是否允许使用麦克风
}
}elseif (voiceBtn.tag ==6){//停止录音
voiceBtn.tag =5;
[self.audioRecorderstop];
}
}
- (void)timerAction{
time =time +1;
timerLab.text = [NSStringstringWithFormat:@"%ld",(long)time];
if (time ==0) {
[timerinvalidate];
}
NSLog(@"%ld",(long)time);
}
/**
* 播放录音
*/
- (void)tapGesture{
if (![self.audioPlayerisPlaying]) {
[self.audioPlayerplay];
}
}
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject];
NSString *filePath = [pathstringByAppendingPathComponent:@"ExceptionAsignVC.mp3"];
NSData *data = [NSDatadataWithContentsOfFile:filePath];
NSString *mp3str = [database64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
mp3str = [mp3str stringByReplacingOccurrencesOfString:@"+"withString:@"%2B"];//后台因为不能识别+,特为转化
库地址: https://pan.baidu.com/s/1o7TAXkA