AndroidAudioConverter 音频格式无法转换问题,关于Android版本升级后语音留言转换工具过时问题。
仿微信语音留言 https://github.com/zhuguohui/WXSoundRecord
wav 转 mp3 使用的是下面这个库
依赖:implementation ‘com.github.adrielcafe:AndroidAudioConverter:0.0.8’
wav 转 mp3 使用的是下面这个库 AndroidAudioConverter
根据Android版本应该是Android12 以后上面这个库就无法正常使用了,因此我门需要修改依赖和方法类!
支持Android13系统 Android13系统权限申请 一下就是升级后的工具类和源码
//wav转mp3的工具
implementation 'com.arthenica:mobile-ffmpeg-full:4.4'
package com.example.recordlib.utli;
import static com.arthenica.mobileffmpeg.Config.RETURN_CODE_SUCCESS;
import android.content.Context;
import android.os.Environment;
import com.arthenica.mobileffmpeg.ExecuteCallback;
import com.arthenica.mobileffmpeg.FFmpeg;
import java.io.File;
import java.io.FileNotFoundException;
/**
* @author xtxiaolu
* @description: 录音文件转换器
* @date :2023/6/6
*/
public class AudioConverter {
private Context mContext;
private File audioFile;
private AudioFormat format;
private ConversionCallback callback;
private AudioConverter(Context context) {
this.mContext = context;
}
public static AudioConverter with(Context context) {
return new AudioConverter(context);
}
public AudioConverter setFile(File originalFile) {
this.audioFile = originalFile;
return this;
}
public AudioConverter setFormat(AudioFormat format) {
this.format = format;
return this;
}
public AudioConverter setCallback(ConversionCallback callback) {
this.callback = callback;
return this;
}
public void convert() {
if (audioFile == null || format == null || callback == null) {
throw new IllegalArgumentException("Incomplete conversion parameters.");
}
if (!audioFile.exists()) {
callback.onConversionFailed(new FileNotFoundException("Audio file not found."));
return;
}
File outputDirectory = mContext.getExternalFilesDir(Environment.DIRECTORY_MUSIC);
File outputFile = new File(outputDirectory, "output.mp3");
String[] command = getConversionCommand(audioFile.getAbsolutePath(), outputFile.getAbsolutePath(), format);
try {
FFmpeg.executeAsync(command, new ExecuteCallback() {
@Override
public void apply(final long executionId, final int returnCode) {
if (returnCode == RETURN_CODE_SUCCESS) {
callback.onConversionCompleted(outputFile.getAbsolutePath());
} else {
callback.onConversionFailed(new Exception("Conversion failed. Return code: " + returnCode));
}
}
});
} catch (Exception e) {
callback.onConversionFailed(e);
}
}
private String[] getConversionCommand(String inputPath, String outputPath, AudioFormat format) {
String audioCodec = format == AudioFormat.MP3 ? "libmp3lame" : "libfaac";
String[] command = {
"-y",
"-i",
inputPath,
"-c:a",
audioCodec,
outputPath
};
return command;
}
public enum AudioFormat {
AMR,
MP3
}
public interface ConversionCallback {
void onConversionCompleted(String outputPath);
void onConversionFailed(Exception exception);
}
}
上面是修改后的工具类
如果需要Android源码的 可点击下载 记得点赞收藏哦!
https://gitee.com/xtxiaolu/wxrecord.git