简体   繁体   English

me脚的编码iOS排队

[英]Lame Encoding iOS in queue

I'm trying to convert a WAV file into MP3 file using LAME. 我正在尝试使用LAME将WAV文件转换为MP3文件。

I am using this code. 我正在使用此代码。 I want to do this in background (or in a queue). 我想在后台(或队列中)执行此操作。 As input file is large, it can take the full control to it till finishing. 由于输入文件很大,因此可以完全控制它直到完成。 Can anybody help me to do so? 有人可以帮我吗?

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);

I would use a serial NSOperationQueue to do the encoding. 我将使用串行NSOperationQueue进行编码。

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;
#ifdef __IPHONE_8_0
queue.qualityOfService = /* a NSQualityOfService */;
#endif

You can add jobs like this: 您可以像这样添加作业:

[queue addOperationWithBlock:^{

    /* Encoding code goes here */

    dispatch_async(dispatch_get_main_queue(), ^{

        /* Report operation status on completion (delegate, notification, etc...)

    }];

}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM