Tool encapsulation for PCM conversion MP3 and pcmmp3

Source: Internet
Author: User

Tool encapsulation for PCM conversion MP3 and pcmmp3

Tool encapsulation for PCM conversion MP3

 

Description

1. Simple encapsulation of PCM to MP3.

2. Use the https://github.com/wuqiong/mp3lame-for-iOS to generate a 64-bit lame library.

 

Source code

Https://github.com/YouXianMing/iOS-General-Tools in 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 shocould 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), ^ {@ tr Y {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}filepath 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 = cursor (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

 

Details

To remove the warning of PcmToMp3Manager, addWno-shorten-64-to-32You can.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.