我尝试使用LAME将WAV文件转换为MP3文件。
我正在使用此代码。 我想在后台(或队列中)这样做。由于输入文件很大,它可以完全控制它直到完成。有人可以帮我这么做吗?
int read, write;
FILE *pcm = fopen([mergeFile 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);
NSLog(@"");
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);
} while (read != 0);
lame_close(lame);
fclose(mp3);
fclose(pcm);
答案 0 :(得分:0)
我会使用序列号NSOperationQueue进行编码。
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;
#ifdef __IPHONE_8_0
queue.qualityOfService = /* a NSQualityOfService */;
#endif
您可以添加以下作业:
[queue addOperationWithBlock:^{
/* Encoding code goes here */
dispatch_async(dispatch_get_main_queue(), ^{
/* Report operation status on completion (delegate, notification, etc...)
}];
}];