项目有录音功能,本来是录制的arm格式的文件,需求让改成MP3格式,折腾半天发现android本身做不到这一点,只能借助ndk来完成,下面是ndk的配置级MP3格式文件的录制
一、ndk介绍及下载:http://wear.techbrood.com/tools/sdk/ndk/index.html
二、接下来是android studio配置ndk
1、project structure里面配置NDK路径
2.src下的build.gradle里defaultConfig下添加
ndk {
moduleName"mp3lame"
ldLibs "log", "z", "m"
abiFilters "armeabi", "armeabi-v7a"
}
3.
gradle.properties最后添加
android.useDeprecatedNdk=true
4.从https://github.com/talzeus/AndroidMp3Recorder中demo下载下来,jni文件夹拷贝到main下,audiorecoder文件夹拷贝到项目里,
libmp3lame下的.c和.h文件名及文件里面的一些地方要改成自己的包名
5.build make project
此时build>intermediates>ndk>debug下lib和obj下应该都有相应的.so文件了
三、调用MP3recoder录制MP3格式的录音
点击按钮开始录音
mRecordBtn.setOnTouchListener(new VoiceTouchListen());
录音结果及录音完成回调
final Mp3Recorder recorder = new Mp3Recorder();
class VoiceTouchListen implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (!FileUtils.checkSDcard()) {
Toast.makeText(context, "发送语音需要sd卡支持!", Toast.LENGTH_SHORT).show();
return false;
}
v.setPressed(true);
handler.postDelayed(runnable, 1000);
// 开始录音
try {
recorder.startRecording();
} catch(IOException e) {
Log.d("MainActivity", "Start error");
}
return true;
case MotionEvent.ACTION_MOVE: {
return true;
}
case MotionEvent.ACTION_UP:
v.setPressed(false);
try {
// if (event.getY() < 0) {// 放弃录音
// recordManager.cancelRecording();
// L.d("放弃发送语音");
// } else {
handler.removeCallbacks(runnable);
try {
recorder.stopRecording();
} catch(IOException e) {
Log.d("MainActivity", "Stop error");
}
//int recordTime = recordManager.stopRecording();
if (recLen > 1) {
filePath = recorder.getRecordFilePath();
//filePath = recordManager.getRecordFilePath(IConst.Cache.RECORD, SDApp.getUserId());
mPlayBtn.setImageResource(R.drawable.shop_log_record_play);
} else {// 录音时间过短,则提示录音过短的提示
Toast.makeText(context, "录音时间过短!", Toast.LENGTH_SHORT).show();
filePath = null;
}
// }
} catch (Exception e) {
e.printStackTrace();
}
return true;
default:
return false;
}
}
}
public static interface OnRecordFinishListener {
public void onRecordFinish(String filePath);
}
private int recLen = 0;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
recLen++;
mState.setText("长度 : " + recLen + "秒");
handler.postDelayed(this, 1000);
}
};
if (playManager.isPlaying()) {
playManager.stopPlayback();
} else {
playManager.playRecording(filePath, true);
}
在OnRecordFinishListener回调中上传文件
public AddRecordDialog(Context context, OnRecordFinishListener listener) {
this.context = context;
this.listener = listener;
}
AddRecordDialog dialog = new AddRecordDialog(context, new AddRecordDialog.OnRecordFinishListener() {
@Override
public void onRecordFinish(String filePath) {
//上传文件
}
});
dialog.show();
private void playLogVoice(View view, ShopLog log) {
ImageView img = (ImageView) view.findViewById(R.id.voiceImg);
final AnimationDrawable anim = (AnimationDrawable) img.getDrawable();
//如果在播放其他音频 则关闭
String dir = IConst.Cache.RECORD + Md5Encryption.getMD5(getUserId());
String fileName = null;
fileName = log.getFile_path().substring(log.getFile_path().lastIndexOf("/")+1);
if(playManager==null){
playManager = PlayManager.getInstance(context);
}
if(TextUtils.isEmpty(dir + File.separator + fileName)){
return;
}
if (playManager.isPlaying() && !playManager.isPlaying(dir + File.separator + fileName)) {
playManager.stopPlayback();
L.d("停止播放其他");
}
//监听播放 更新动画
playManager.setOnPlayChangeListener(new IOnPlayChangeListener() {
@Override
public void onPlayStart() {
L.d("start");
anim.start();
}
@Override
public void onPlayStop() {
L.d("stop");
anim.stop();
}
});
if (new File(dir).exists() && new File(dir + File.separator + fileName).exists()) {
L.d("0000000000--0");
if (playManager.isPlaying()) {
L.d("0000000000--1");
playManager.stopPlayback();
} else {
playManager.playRecording(dir + File.separator + fileName, true);
}
} else {
L.d("11111111111--0");
if(log.getFile_path()==null){return;}
L.d("log.getFilePath()--"+log.getFile_path());
L.d("log.getFileName()--"+fileName);
Request request = new Request.Builder().url(path).build();
RequestTask requestTask = new RequestTask<>(request, savePath, fileName,
new RequestCallBack() {
@Override
public void onSuccess(ResponseInfo responseInfo) {
L.d(responseInfo.result.getAbsolutePath());
playManager.playRecording(responseInfo.result.getAbsolutePath(), true);
}
@Override
public void onFinish() {
}
@Override
public void onFailure(HttpException error) {
showToast(error.errorMsg);
}
}
static {
System.loadLibrary("mp3lame");
}