PCM轉換MP3的工具封裝,pcmmp3封裝

來源:互聯網
上載者:User

PCM轉換MP3的工具封裝,pcmmp3封裝

PCM轉換MP3的工具封裝

 

說明

1. 對 PCM 轉 MP3 進行了簡單的封裝.

2. 使用 https://github.com/wuqiong/mp3lame-for-iOS 產生支援64位的 lame 庫.

 

源碼

https://github.com/YouXianMing/iOS-General-Tools 中的 PCM-to-MP3

////  PcmToMp3Manager.h//  RecordMusic////  Created by YouXianMing on 16/7/28.//  Copyright © 2016年 YouXianMing. All rights reserved.////  Lame-for-iOS https://github.com/wuqiong/mp3lame-for-iOS//#import <Foundation/Foundation.h>@class PcmToMp3Manager;@protocol PcmToMp3ManagerDelegate <NSObject>@optional/** *  Did convert the pcm to mp3. * *  @param manager   The PcmToMp3Manager object. *  @param sucess    Sucess or not. *  @param errorInfo Error info. */- (void)didConvertPcmToMp3:(PcmToMp3Manager *)manager sucess:(BOOL)sucess errorInfo:(NSString *)errorInfo;@end/** *  In "Build Phases", You can add '-Wno-shorten-64-to-32' to the file 'PcmToMp3Manager.m' to ignore the warning. */@interface PcmToMp3Manager : NSObject/** *  The PcmToMp3Manager's delegate. */@property (nonatomic, weak) id <PcmToMp3ManagerDelegate> delegate;/** *  The pcm file's path. */@property (nonatomic, strong) NSString *pcmFilePath;/** *  The mp3 file's path you specified. */@property (nonatomic, strong) NSString *mp3FilePath;/** *  Before you start convert, you should specified the pcm file's path. */- (void)startConvert;@end
////  PcmToMp3Manager.m//  RecordMusic////  Created by YouXianMing on 16/7/28.//  Copyright © 2016年 YouXianMing. All rights reserved.//#import "PcmToMp3Manager.h"#import <lame/lame.h>@implementation PcmToMp3Manager- (void)startConvert {        NSParameterAssert(self.pcmFilePath);        BOOL isDirectory = NO;    BOOL isExist     = [[NSFileManager defaultManager] fileExistsAtPath:self.pcmFilePath isDirectory:&isDirectory];        if (isExist && isDirectory == NO) {                dispatch_async(dispatch_get_global_queue(0, 0), ^{                        @try {                                int read, write;                                FILE *pcm = fopen([self.pcmFilePath cStringUsingEncoding:1], "rb");  //source                fseek(pcm, 4*1024, SEEK_CUR);                                        //skip file header                FILE *mp3 = fopen([self.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, 44100);                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) {                                if (self.delegate && [self.delegate respondsToSelector:@selector(didConvertPcmToMp3:sucess:errorInfo:)]) {                                        dispatch_async(dispatch_get_main_queue(), ^{                                                [self.delegate didConvertPcmToMp3:self sucess:NO errorInfo:exception.description];                    });                }                            } @finally {                                if (self.delegate && [self.delegate respondsToSelector:@selector(didConvertPcmToMp3:sucess:errorInfo:)]) {                                        dispatch_async(dispatch_get_main_queue(), ^{                                                [self.delegate didConvertPcmToMp3:self sucess:YES errorInfo:nil];                    });                }            }        });            } else {                if (self.delegate && [self.delegate respondsToSelector:@selector(didConvertPcmToMp3:sucess:errorInfo:)]) {                        dispatch_async(dispatch_get_main_queue(), ^{                                [self.delegate didConvertPcmToMp3:self sucess:NO errorInfo:[NSString stringWithFormat:@"'%@' not exist.", self.pcmFilePath]];            });        }    }}@end

 

細節

為了去除 PcmToMp3Manager 的 warning, 在檔案 PcmToMp3Manager.m 添加 -Wno-shorten-64-to-32 即可

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.