记录:解决MediaRecorder stop问题


package com.yconme.callphone.Activity;

import android.media.MediaRecorder;

import android.os.Environment;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import com.yconme.callphone.Utils.ToastUtils;

import java.io.File;

import java.io.IOException;

import static android.R.attr.name;

/**

* Created by saksamaa on 2017/6/14.

*/

public class PhoneThead extends Handler {

private MediaRecorder mRecorder = null;

private String dir;

private String s;

@Override

public void handleMessage(Message msg) {

//        mRecorder = new MediaRecorder();

//        Recorder recorder = new Recorder();

switch (msg.what) {

//开始录音

case 1:

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

String sdpath = Environment.getExternalStorageDirectory() + "/";

dir = sdpath + "Luyin";

File file = new File(dir);

if (!file.exists()) {

file.mkdir();

//                        fileName = new File(dir, "创建.MP3");

} else {

}

}

//设置sdcard的路径

File FileName = Environment.getExternalStorageDirectory();

s = FileName.getPath() + "/Luyin";

s += "/audiorecordtest3.MP3";

if (mRecorder == null) {

mRecorder = new MediaRecorder();

mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

mRecorder.setOutputFile(s);

try {

mRecorder.prepare();

mRecorder.start();

} catch (IllegalStateException e) {

System.out.print(e.getMessage());

} catch (IOException e) {

System.out.print(e.getMessage());

}

} else {

mRecorder = new MediaRecorder();

mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

mRecorder.setOutputFile(s);

try {

mRecorder.prepare();

} catch (IllegalStateException | IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

mRecorder.start();

}

//                    mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);

//                mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

//                mRecorder.setOutputFile(s);

//                mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

//                try {

//                    mRecorder.prepare();

//                } catch (IOException e) {

//                    Log.e("TAG", "prepare() failed");

//                }

//                mRecorder.start();

break;

case 2:

Log.e("TAG", "handleMessagehandleMessagehandleMessagehandleMessagehandleMessage: ");

//解决办法在这里

if (mRecorder != null) {

try {

mRecorder.stop();

mRecorder.release();

} catch (IllegalStateException e) {

e.printStackTrace();

}

}

break;

}

}

}

你可能感兴趣的:(记录:解决MediaRecorder stop问题)