代码地址:https://download.csdn.net/download/qq_14931305/10803169
Bass库官网:http://www.un4seen.com/
Bass库参考文档:http://www.un4seen.com/doc/#bass/bass.html
集成请参考我之前的博客:https://blog.csdn.net/qq_14931305/article/details/84374301
需要注意的是,此处集成还需添加 libbassmix.so
无论是Mp3转Wav还是Channel/Samplerate的修改都可采用以下方法,操作步骤都是将文件先转成pcm,然后再转成wav文件
BassOperationHelper类核心代码
static boolean mp3ToPcm(String pathMp3, String pathPcm,int wavSize) {
if (!BASS.BASS_Init(-1,mSampleRate,0)) {
Error("cannot BASS_Init");
return false;
}
// 重采样后的流
int mixedStream = BASSmix.BASS_Mixer_StreamCreate(mSampleRate, mChannels, BASS.BASS_STREAM_DECODE | BASSmix.BASS_MIXER_END);
if (mixedStream == 0){
Error("BASS_Mixer_StreamCreate error");
return false;
}
// 原音频流
int srcStream = BASS.BASS_StreamCreateFile(pathMp3, 0, 0, BASS.BASS_STREAM_DECODE);
if (srcStream == 0){
Error("BASS_StreamCreateFile error");
return false;
}
//将源音频流加入到Mixer里面,以实现重采样转换
boolean flag = BASSmix.BASS_Mixer_StreamAddChannelEx(mixedStream,srcStream,0,0,wavSize);// plug in the source
//如果全部转换成wav文件,请采用下面这个方法
//boolean flag = BASSmix.BASS_Mixer_StreamAddChannelEx(mixedStream,srcStream,0);// plug in the source
if (!flag){
Error("BASS_Mixer_StreamAddChannel error");
return false;
}
try {
// 写到output流
ByteBuffer byteBuffer = ByteBuffer.allocate(wavSize);
FileOutputStream fos = new FileOutputStream(pathPcm);
while (BASS.BASS_ChannelIsActive(mixedStream) != BASS.BASS_ACTIVE_STOPPED)
{
BASS.BASS_ChannelGetData(mixedStream, byteBuffer, byteBuffer.capacity());
fos.write(byteBuffer.array());
}
BASSmix.BASS_Mixer_ChannelRemove(mixedStream);
BASSmix.BASS_Mixer_ChannelRemove(srcStream);
BASS.BASS_Free();
} catch (IOException e) {
e.printStackTrace();
Error(e.getMessage());
return false;
}
return true;
}
MainActivity类调用
private static final String oldFilePath ="/storage/emulated/0/夏季.wav";
private static final String newFilePath ="/storage/emulated/0/夏季"+System.currentTimeMillis()+".wav";
BassOperationHelper.modifyChannelOrSample(oldFilePath, newFilePath, 1024 * 3000, new BassOperationHelper.Mp3ToWavCallBack() {
@Override
public void onComplete(boolean isSuccess) {
//mp3转pcm
Log.e("BassOperationHelper","oldFilePath= "+oldFilePath);
Log.e("BassOperationHelper","newFilePath= "+newFilePath);
if (isSuccess){
Toast.makeText(MainActivity.this,"文件操作成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"文件操作失败",Toast.LENGTH_SHORT).show();
}
}
});
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/184364.html原文链接:https://javaforall.cn