lame静态库使用

来源:互联网 发布:淘宝怎样取消退款 编辑:程序博客网 时间:2024/06/08 07:52

拖入上篇博文制作的lame静态库到工程,包括libmp3lame.a lame.h两个文件,如下图左侧


附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);            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 {        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 UIKitimport 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),// 通道数            AVEncoderAudioQualityKey: NSNumber(value: Int32(AVAudioQuality.min.rawValue) as Int32)// 录音质量        ]        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)    }}


0 0
原创粉丝点击