1. 程式人生 > >android錄音MP3格式檔案

android錄音MP3格式檔案

專案有錄音功能,本來是錄製的arm格式的檔案,需求讓改成MP3格式,折騰半天發現android本身做不到這一點,只能藉助ndk來完成,下面是ndk的配置級MP3格式檔案的錄製

二、接下來是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

libmp3lame下的.c和.h檔名及檔案裡面的一些地方要改成自己的包名



libs下的檔案都拷貝到自己專案的libs下

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);
    }

handler記錄長度
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<File> requestTask = new RequestTask<>(request, savePath, fileName, 
new RequestCallBack<File>() {
                @Override
                public void onSuccess(ResponseInfo<File> 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);
                }
            }

); requestTask.execute(); 備註:如果MP3Recorder類中沒有呼叫本地方法需加上(android呼叫本地方法)
static {
		System.loadLibrary("mp3lame");
	}