1.編碼工具類 Mp3Encoder.hpp & Mp3Encoder.cpp
Mp3Encoder.hpp 標頭檔案程式碼:
//
// Mp3Encoder.hpp
// RFAVP
//
// Created by 龐日富 on 2021/1/25.
//
#ifndef Mp3Encoder_hpp
#define Mp3Encoder_hpp
#include <stdio.h>
#import "lame.h"
// 宣告一個類:Mp3Encoder ,
// Mp3Encoder 類提供一個公開方法:
class Mp3Encoder
{
private:
FILE* pcmFile;
FILE* mp3File;
lame_t lameClient;
public:
// Mp3Encoder(); //方法未實現,先註釋
// ~Mp3Encoder(); //方法未實現,先註釋
int Init(const char* pcmFilePath, const char* mp3FilePath, int sampleRate , int chanels, int bitRate);
void encode();
void destory();
};
#endif /* Mp3Encoder_hpp */
複製程式碼
Mp3Encoder.cpp 實現檔案程式碼:
//
// Mp3Encoder.cpp
// RFAVP
//
// Created by 龐日富 on 2021/1/25.
//
#include "Mp3Encoder.hpp"
int Mp3Encoder::Init(const char *pcmFilePath, const char *mp3FilePath, int sampleRate, int chanels, int bitRate){
int ret = -1;
pcmFile = fopen(pcmFilePath, "rb");
if (pcmFile) {
mp3File = fopen(mp3FilePath, "wb");
if (mp3File) {
lameClient = lame_init();
lame_set_in_samplerate(lameClient, sampleRate);
lame_set_out_samplerate(lameClient, sampleRate);
lame_set_num_channels(lameClient, chanels);
lame_set_brate(lameClient, bitRate/1000);
lame_init_params(lameClient);
ret = 0;
}
}
return ret;
}
void Mp3Encoder::encode() {
int bufferSize = 1024 * 256;
short* buffer = new short[bufferSize / 2];
short* leftBuffer = new short[bufferSize / 4];
short* rightBuffer = new short[bufferSize / 4];
unsigned char* mp3_buffer = new unsigned char[bufferSize];
size_t readBufferSize = 0;
while ((readBufferSize = fread(buffer, 2, bufferSize / 2, pcmFile)) > 0) {
for (int i = 0; i < readBufferSize; i++) {
if (i % 2 == 0) {
leftBuffer[i / 2] = buffer[i];
} else {
rightBuffer[i / 2] = buffer[i];
}
}
size_t wroteSize = lame_encode_buffer(lameClient, (short int *) leftBuffer, (short int *) rightBuffer, (int)(readBufferSize / 2), mp3_buffer, bufferSize);
fwrite(mp3_buffer, 1, wroteSize, mp3File);
}
delete [] buffer;
delete [] leftBuffer;
delete [] rightBuffer;
delete [] mp3_buffer;
}
void Mp3Encoder::destory(){
if (pcmFile) {
fclose(pcmFile);
}
if (mp3File) {
fclose(mp3File);
lame_close(lameClient);
}
}
複製程式碼
iOS整合
FirstVC.mm 檔案中呼叫編碼工具類的具體用法
//
// FirstVC.m
// RFAVP
//
// Created by PRF on 2021/1/22.
//
#import "FirstVC.h"
#import <Masonry/Masonry.h>
#include "Mp3Encoder.hpp"
#import "CommonUtil.h"
@interface FirstVC ()
@property (nonatomic,strong) UIButton *EncodeBtn;
@end
@implementation FirstVC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"音視訊進階";
[self.EncodeBtn addTarget:self action:@selector(encodeClick:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)encodeClick:(UIButton *)btn{
NSLog(@"按鈕點選事件");
Mp3Encoder *encoder = new Mp3Encoder();
// 原始檔的的路徑
const char* pcmFilePath = [[CommonUtil bundlePath:@"vocal.pcm"] cStringUsingEncoding:NSUTF8StringEncoding];
// 要生成的mp3檔案的路徑
const char *mp3FilePath = [[CommonUtil documentsPath:@"vocal.mp3"] cStringUsingEncoding:NSUTF8StringEncoding];
int sampleRate = 44100;
int channels = 2;
int bitRate = 128 * 1024;
// 初始化解碼器,傳入原始檔路徑,生成的檔案路徑,取樣頻率,聲道數,位元速率
encoder->Init(pcmFilePath, mp3FilePath, sampleRate, channels, bitRate);
// 編碼
encoder->encode();
//關閉檔案
encoder->destory();
delete encoder;
NSLog(@"Encode Success");
NSLog(@"file save path : %s",mp3FilePath);
}
-(UIButton *)EncodeBtn{
if (!_EncodeBtn) {
_EncodeBtn = [UIButton new];
[self.view addSubview:_EncodeBtn];
[_EncodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.mas_equalTo(0);
make.width.mas_equalTo(100);
make.height.mas_equalTo(50);
}];
_EncodeBtn.backgroundColor = [UIColor blueColor];
[_EncodeBtn setTitle:@"音視訊測試" forState:UIControlStateNormal];
}
return _EncodeBtn;
}
@end
複製程式碼