最近公司要求想在嵌入的網(wǎng)頁里調(diào)用原聲的的錄音功能,并且吧錄音上傳到服務(wù)器,然后就開始各種趴資料啦,畢竟是小菜??嘛臼节。以下就是一些總結(jié)吧:
首先當(dāng)然是js與oc的互相調(diào)用了可以參考一下我的上一篇.
這是官方文檔AVAudioRecorder Class Reference
關(guān)于錄音的方法就不詳細介紹了可以參考這里接剩。
AVAudioRecorder *audioRecorder=[[AVAudioRecorder alloc]initWithURL:url settings:setting error:&error];
在創(chuàng)建錄音時切厘,我們有填寫保存路徑,在錄音結(jié)束時會進入代理方法
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag;
我們需要在結(jié)束時對錄音進行轉(zhuǎn)換格式:
github上的Demo這是轉(zhuǎn)為mp3格式和錄音的demo懊缺,感謝這位大大疫稿,里面的轉(zhuǎn)碼lame可用。需要我們手動導(dǎo)入lame.h與libmp3lame.a文件鹃两,然后引入lame.h就可以進行轉(zhuǎn)換啦
- (void)toMp3
{
// 錄音文件路徑
NSString *cafFilePath =[NSTemporaryDirectory() stringByAppendingString:kRecordAudioFile];
// mp3文件名
NSString *mp3FileName = @"Mp3File";
mp3FileName = [mp3FileName stringByAppendingString:@".mp3"];
// mp3保存路徑
NSString *mp3FilePath = [[NSHomeDirectory() stringByAppendingFormat:@"/Documents/"] stringByAppendingPathComponent:mp3FileName];
NSLog(@"mp3FilePath ---- %@", mp3FilePath);
@try {
int read, write;
FILE *pcm = fopen([cafFilePath cStringUsingEncoding:1], "rb"); //source
fseek(pcm, 4*1024, SEEK_CUR); //skip file header
FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb"); //output
const int PCM_SIZE = 8192;
const int MP3_SIZE = 8192;
short int pcm_buffer[PCM_SIZE*2];
unsigned char mp3_buffer[MP3_SIZE];
lame_t lame = lame_init();
lame_set_in_samplerate(lame, 11025.0);
lame_set_VBR(lame, vbr_default);
lame_init_params(lame);
do {
read = fread(pcm_buffer, 2*sizeof(short int), 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]);
}
@finally {
// 轉(zhuǎn)換完成 輸出大小
NSInteger fileSize = [self getFileSize:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@", kRecordAudioFile]];
NSLog(@"RecordedFile ------- %@", [NSString stringWithFormat:@"%ld kb", fileSize/1024]);
// 轉(zhuǎn)換成功后的操作
[self convertMp3Finish];
}
}
計算文件大小的方法
- (NSInteger) getFileSize:(NSString*) path
{
NSFileManager * filemanager = [[NSFileManager alloc]init];
if([filemanager fileExistsAtPath:path]){
NSDictionary * attributes = [filemanager attributesOfItemAtPath:path error:nil];
NSNumber *theFileSize;
if ( (theFileSize = [attributes objectForKey:NSFileSize]) )
return [theFileSize intValue];
else
return -1;
}
else
{
return -1;
}
}
以上基本為參考github上的Demo沒啥好提的遗座,感謝這位大神。
然后這里提一下我犯傻的地方,在上傳數(shù)據(jù)的時候我用了下面這個方法:
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@", @"Mp3File.mp3"]] options:nil error:nil];
我一直以為路徑轉(zhuǎn)化為url就可以了 結(jié)果是不行的 這個方法是請求網(wǎng)絡(luò)數(shù)據(jù)的俊扳。本底數(shù)據(jù)要用下面這個方法途蒋。
NSData *data = [[NSData alloc] initWithContentsOfFile:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@", @"Mp3File.mp3"]];
嗯 大致就是這樣啦。