如何在iPhone上使用LAME编码成.mp3时显示进度条
How to show a progress bar while encoding to .mp3, using LAME on the iPhone?
我使用 LAME
在我的iPhone应用程序上编码.caf
音频文件到.mp3
。
一切工作完美,我只是有一个困难的时间试图找出如何记录音频文件转换为.mp3
的进度,同时显示与UIProgressView
的用户的进度。
我使用以下代码(对Deepak Soni编写的原始代码进行了一些修改):
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%sn", 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);
}
我不得不使用lame_set_num_samples
,只有这样lame_get_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;
});
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 使用 Struct 进行 mp3 播放器模拟C++(不需要播放 mp3 的唯一显示列表)
- 无法在QTreeView中显示.mp3文件
- 如何在iPhone上使用LAME编码成.mp3时显示进度条
- MP3 文件长度显示不正确