我想要将两个或更多的.wav文件合并到一个文件中,然后将其转换为.mp3,我想在
Swift中完成(或者至少有选项将其包含在swift项目中).
在swift中合并两个.wav文件是没有问题的. Here is my example现在我不知道如何添加lame库来快速的项目以及如何使用它(如何改变客观的c跛脚代码使用语法,以便在swift中使用它).
我坚持在迅速,所以我尝试Lame库与Objective C.我找到了将.caf转换为.mp3的示例代码,所以我试过了.这是我试过的:
- (void) toMp3 { NSString *cafFilePath = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"caf"]; NSString *mp3FileName = @"Mp3File"; mp3FileName = [mp3FileName stringByAppendingString:@".mp3"]; NSString *mp3FilePath = [[NSHomeDirectory() stringByAppendingFormat:@"/Documents/"] stringByAppendingPathComponent:mp3FileName]; NSLog(@"%@",mp3FilePath); @try { int read,write; FILE *pcm = fopen([cafFilePath cStringUsingEncoding:1],"rb"); //source fseek(pcm,4*1024,SEEK_CUR); //skip file header FILE *mp3 = fopen([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_SIZE); fwrite(mp3_buffer,write,1,mp3); } while (read != 0); lame_close(lame); fclose(mp3); fclose(pcm); } @catch (NSException *exception) { NSLog(@"%@",[exception description]); } @finally { [self performSelectorOnMainThread:@selector(convertMp3Finish) withObject:nil waitUntilDone:YES]; } } - (void) convertMp3Finish { }
但是这样做的结果只是带有噪音的.mp3.
> Here is example .caf file.
> And here is result .mp3 file.
所以我需要修复我的三个问题:
>在Objective C中从caf创建正确的mp3
>更改代码以将其用于wav文件
>并将其更改为能够在Swift中使用它
我知道iOS中编码和转换mp3有很多问题,但是我找不到Swift的例子,我找不到工作Objective C代码的例子(只是上面的代码).感谢帮助
解决方法
我们有专门的类来从/ AVAssetReader和AVAssetWriter的文件读取/写入媒体,并借助AVAssetExportSession可以将其导出为mp3文件.
否则你可以使用 https://github.com/michaeltyson/TPAACAudioConverter
否则你可以使用 https://github.com/michaeltyson/TPAACAudioConverter