【发布时间】:2015-10-05 18:43:58
【问题描述】:
我想将两个或多个 .wav 文件合并为一个,然后将其转换为 .mp3,我想在 Swift 中完成此操作(或者至少可以选择将其包含到 swift 项目中)。
在 swift 中合并两个 .wav 文件不是问题。 Here is my example 现在我不知道如何将 lame 库添加到 swift 项目以及如何使用它(如何更改目标 c lame 代码使用语法以在 swift 中使用它)。
我坚持使用 swift,所以我尝试了带有 Objective C 的 Lame 库。我找到了将 .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_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 {
[self performSelectorOnMainThread:@selector(convertMp3Finish)
withObject:nil
waitUntilDone:YES];
}
}
- (void) convertMp3Finish
{
}
但这样做的结果只是带有噪音的 .mp3。
所以我需要解决我的三个问题:
- 在 Objective C 中从 caf 创建正确的 mp3
- 更改代码以将其用于 wav 文件
- 并将其更改为能够在 Swift 中使用它
我知道在 iOS 中有很多关于编码和转换 mp3 的问题,但我找不到 Swift 示例,也找不到使用 Objective C 代码的示例(只是上面的代码)。感谢您的帮助
【问题讨论】:
-
在此代码中计数赏金如果更多的高内存 wav 文件然后崩溃代码...不将 wav 转换为 mp3 文件