lame静态库使用
拖入上篇博文制作的lame靜態(tài)庫到工程,包括libmp3lame.a lame.h兩個文件,如下圖左側(cè)
附lame使用工具類LameTool
#import <Foundation/Foundation.h>@interface LameTool : NSObject+ (NSString *)audioToMP3: (NSString *)sourcePath isDeleteSourchFile: (BOOL)isDelete;@end#import "LameTool.h" #import "lame.h"@implementation LameTool+ (NSString *)audioToMP3: (NSString *)sourcePath isDeleteSourchFile:(BOOL)isDelete{NSString *inPath = sourcePath;NSFileManager *fm = [NSFileManager defaultManager];if (![fm fileExistsAtPath:sourcePath]){NSLog(@"file path error!");return @"";}NSString *outPath = [[sourcePath stringByDeletingPathExtension] stringByAppendingString:@".mp3"];@try {int read, write;FILE *pcm = fopen([inPath cStringUsingEncoding:1], "rb");fseek(pcm, 4*1024, SEEK_CUR);FILE *mp3 = fopen([outPath cStringUsingEncoding:1], "wb");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 {size_t size = (size_t)(2 * sizeof(short int));read = fread(pcm_buffer, size, PCM_SIZE, pcm);if (read == 0)write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);elsewrite = 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 {NSLog(@"mp3 build success!");if (isDelete) {NSError *error;[fm removeItemAtPath:sourcePath error:&error];if (error == nil){NSLog(@"source file is deleted!");}}return outPath;} } @end
制作一個swift使用上面OC工具類的橋接文件lameDemo-Bridging-Header.h并輸入一條語句
#import "LameTool.h"
?使用如下
// // ViewController.swift // lameDemo // // Created by targetcloud on 2016/11/29. // Copyright ? 2016年 targetcloud. All rights reserved. //import UIKit import AVFoundationclass ViewController: UIViewController {lazy var record: AVAudioRecorder? = {let url = URL(string: "/Users/targetcloud/Desktop/test.caf")let configDic: [String: AnyObject] = [AVFormatIDKey: NSNumber(value: Int32(kAudioFormatLinearPCM) as Int32),// 編碼格式AVSampleRateKey: NSNumber(value: 11025.0 as Float),// 采樣率AVNumberOfChannelsKey: NSNumber(value: 2 as Int32),// 通道數(shù)AVEncoderAudioQualityKey: NSNumber(value: Int32(AVAudioQuality.min.rawValue) as Int32)// 錄音質(zhì)量]do {let record = try AVAudioRecorder(url: url!, settings: configDic)record.prepareToRecord()return record}catch {print(error)return nil}}()override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {record?.record()}override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {record?.stop()let path = LameTool.audio(toMP3: "/Users/targetcloud/Desktop/test.caf", isDeleteSourchFile: true)print(path)}}總結(jié)
- 上一篇: msrcr图像增强算法 matlab,图
- 下一篇: [ delphi ] AES-256-E