欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

lame静态库使用

发布时间:2023/12/16 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 lame静态库使用 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

拖入上篇博文制作的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);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),// 通道数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)}}

总结

以上是生活随笔为你收集整理的lame静态库使用的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。