美文网首页
解决安卓播放ios录音转mp3后时长不准问题

解决安卓播放ios录音转mp3后时长不准问题

作者: gleeeli | 来源:发表于2019-01-25 20:30 被阅读4次
    • (BOOL)switchPCMToMp3Path:(NSString *)mp3FilePath {
      NSString *filePath = self.filePath;

      NSFileManager *manager = [NSFileManager defaultManager];
      long long l = [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
      if (!filePath ||l==0) {
      return NO;
      }

      @try {
      int read, write;

        FILE *pcm = fopen([filePath cStringUsingEncoding:1], "rb");//被转换的文件
        fseek(pcm, 4*1024, SEEK_CUR);
        FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb");//转换后文件的存放位置
        
        int channel = kDefaultChannel;
        const int PCM_SIZE = 8192;
        const int MP3_SIZE = 8192;
        short int pcm_buffer[PCM_SIZE*channel];
        unsigned char mp3_buffer[MP3_SIZE];
        
        lame_t lame = lame_init();
        lame_set_num_channels(lame,channel);
        lame_set_in_samplerate(lame, kDefaultSampleRate);//录音16000
        //关键这一句!!!!!!!!!!!!
        lame_set_VBR_mean_bitrate_kbps(lame, 24);
      

    // lame_set_VBR(lame, vbr_default);//压缩级别参数:
    lame_set_brate(lame,32);/* 比特率 */

        lame_set_mode(lame,MONO);
        
        lame_set_quality(lame,2);/* 2=high  5 = medium  7=low */
        
        lame_init_params(lame);
        
        do {
            read = (int)fread(pcm_buffer, channel*sizeof(short int), PCM_SIZE, pcm);
            if (read == 0)
                write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
            else {
                if (channel == 2) {
                    write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
                }else {//单声道走这
                    write = lame_encode_buffer(lame, pcm_buffer, NULL, read, mp3_buffer, MP3_SIZE);
                }
                
            }
            
            fwrite(mp3_buffer, write, 1, mp3);
            
        } while (read != 0);
        lame_mp3_tags_fid(lame, mp3);
        lame_close(lame);
        fclose(mp3);
        fclose(pcm);
    }
    @catch (NSException *exception) {
        NSLog(@"转码异常:%@",[exception description]);
    }
    @finally {
        
    }
    
    return YES;
    

    }

    相关文章

      网友评论

          本文标题:解决安卓播放ios录音转mp3后时长不准问题

          本文链接:https://www.haomeiwen.com/subject/zubvjqtx.html