在APP中,我們也會遇到調用錄音的功能,那么如何錄音呢?並且在iOS中錄音格式是wav或者caf格式的,和安卓不通用,為了達到通用的效果,我們還需要把他轉換成通用格式。近期我遇到了一個這個功能,我是這樣實現的。通過AVAudioRecorder來實現錄音,並且我設置錄音格式為wav格式。在錄音結束后,通過lame把wav格式轉換為MP3格式。
光說沒有太大作用,我在末尾把封裝好的工具類附上,在錄音工具類中,我只留出三個方法(開始錄音,停止錄音,格式轉換)。
使用代碼:
// 開始錄音 self.tool = [RecordTool sharedInstance]; [self.tool startRecordVoice];
// 停止錄音,並轉化格式
self.tool = [RecordTool sharedInstance]; if (self.tool.mp3SavePath.length != 0) { [self.tool stopRecordVoice]; [self.tool convertWavToMp3:self.tool.savePath withSavePath:self.tool.mp3SavePath]; }
另外我再附上一個播放類,可以用它播放所錄音頻
self.tool = [RecordTool sharedInstance]; if (self.tool.mp3SavePath.length != 0) { [[BTVoicePlayer share]play:self.tool.mp3SavePath]; }
// 獲取音頻時長 AVURLAsset* audioAsset =[AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:self.savePath] options:nil]; CMTime audioDuration = audioAsset.duration; float audioDurationSeconds = CMTimeGetSeconds(audioDuration);
補充:新增修改,做了一些防崩潰處理,可以設置最大錄音時長。RecordTool增強版.zip