iOS錄音后轉換mp3

最近公司要求想在嵌入的網頁里調用原聲的的錄音功能,并且吧錄音上傳到服務器,然后就開始各種趴資料啦,畢竟是小菜??嘛。以下就是一些總結吧:
首先當然是js與oc的互相調用了可以參考一下我的上一篇.

這是官方文檔AVAudioRecorder Class Reference

關于錄音的方法就不詳細介紹了可以參考這里

AVAudioRecorder *audioRecorder=[[AVAudioRecorder alloc]initWithURL:url settings:setting error:&error];

在創建錄音時,我們有填寫保存路徑,在錄音結束時會進入代理方法

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag;

我們需要在結束時對錄音進行轉換格式:
github上的Demo這是轉為mp3格式和錄音的demo,感謝這位大大,里面的轉碼lame可用。需要我們手動導入lame.h與libmp3lame.a文件,然后引入lame.h就可以進行轉換啦

- (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 {
      // 轉換完成 輸出大小
        NSInteger fileSize =  [self getFileSize:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@", kRecordAudioFile]];
        NSLog(@"RecordedFile ------- %@", [NSString stringWithFormat:@"%ld kb", fileSize/1024]);
      // 轉換成功后的操作
        [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沒啥好提的,感謝這位大神。

然后這里提一下我犯傻的地方,在上傳數據的時候我用了下面這個方法:

NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@", @"Mp3File.mp3"]] options:nil error:nil];

我一直以為路徑轉化為url就可以了 結果是不行的 這個方法是請求網絡數據的。本底數據要用下面這個方法。

NSData *data = [[NSData alloc] initWithContentsOfFile:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@", @"Mp3File.mp3"]];

嗯 大致就是這樣啦。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一種新的協議。它實...
    香橙柚子閱讀 24,061評論 8 183
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,067評論 25 708
  • 好久沒炒過這個菜吃了《八秒羊肝》手藝還是沒回潮噠。開飯開飯!
    黃花大小伙CD閱讀 282評論 0 0
  • 我經常說,韓國電影最出彩的地方是赤裸裸的揭露社會的丑惡,幼童奸殺、司法黑暗、社會誤解、蒙冤受屈,這些元素,這部電影...
    唐縱凌閱讀 239評論 0 0
  • 我們仨 端午節期間,無意中翻閱了楊絳先生的著作《我們仨》,情真意切,的確很感人。突然想寫我們仨的故事,卓姑娘(我老...
    從彥閱讀 276評論 2 0