vikeyToy 2013-05-17 05:55 采纳率: 0%
浏览 2673

转码mp3格式时显示进度条。

使用 LAME 将.caf音频文件编码为.mp3文件。

只有一个问题,需要计算转换文件的时间,用UIProgressView给用户显示转换进度,这点实现不了。

NSArray *dirPaths;
NSString *docsDir;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
_mp3FilePath = [docsDir stringByAppendingPathComponent:@"file.mp3"];

NSString* cafFile = [[NSBundle mainBundle] pathForResource:@"background" ofType:@"caf"];
char cstr[512] = {0};
[cafFile getCString:cstr maxLength:512 encoding:NSASCIIStringEncoding];
@try {
    int read, write;
    FILE *pcm = fopen([cafFile cStringUsingEncoding:1], "rb");
    FILE *mp3 = fopen([_mp3FilePath cStringUsingEncoding:1], "wb");
    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);

    NSFileManager *fileManger = [NSFileManager defaultManager];
    NSData *data = [fileManger contentsAtPath:cafFile];
    NSString* str = [NSString stringWithFormat:@"%d K",[data length]/1024];
    NSLog(@"size of caf=%@",str);

    printf("Using LAME v%s\n", get_lame_version());

    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 {
    //Detrming the size of mp3 file
    NSFileManager *fileManger = [NSFileManager defaultManager];
    NSData *data = [fileManger contentsAtPath:_mp3FilePath];
    NSString* str = [NSString stringWithFormat:@"%d K",[data length]/1024];
    NSLog(@"size of mp3=%@",str);
}
  • 写回答

1条回答

  • LPPloveROU 2013-05-17 06:52
    关注

    我建议使用lame_set_num_samples ,像这样:

    int sampleRate = 44100;
    int bitsPerSample = 16;
    int numberOfChannels = 2;
    lame_t lame = lame_init();
    lame_set_in_samplerate(lame, 44100);
    lame_set_VBR(lame, vbr_default);
    lame_init_params(lame);
    NSString* cafFile = [[NSBundle mainBundle] pathForResource:@"background" ofType:@"caf"];
    char cstr[512] = {0};
    [cafFile getCString:cstr maxLength:512 encoding:NSASCIIStringEncoding];
    int fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:cafFile error:nil] fileSize];
    int duration = (fileSize * 8) / (sampleRate * bitsPerSample * numberOfChannels);
    lame_set_num_samples(lame, (duration * sampleRate));
    lame_get_num_samples(lame);
    int percent     = 0;
    int samp_rate   = lame_get_out_samplerate(lame);
    int frameNum    = lame_get_frameNum(lame);
    int totalframes = lame_get_totalframes(lame);
    
    if (frameNum < totalframes) {
            percent = (int) (100. * frameNum / totalframes + 0.5);
        }
        else {
            percent = 100;
        }
    float progress = (float) percent/100;
    dispatch_async(dispatch_get_main_queue(), ^{
           self.percentageLabel.text = [NSString stringWithFormat:@"%d%%",percent];
           self.progressBar.progress =  progress;
    
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值