根据项目需求做一个自定义录音
package com.wswy.cj.recon_record; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Chronometer; import android.widget.ImageButton; import android.widget.TextView; import java.io.File; import java.io.IOException; import java.util.Calendar; import java.util.UUID; import static android.os.Build.VERSION_CODES.BASE; @SuppressLint("ShowToast") public class RecordActivity extends AppCompatActivity implements Runnable { private MediaRecorder mMediaRecorder; private boolean mStartedFlg = false; private boolean isAlive = true; private ImageButton kaishi; private static String PATH_NAME = null; private VoiceLineView voiceLineView; private Chronometer timer; private ImageButton mPlayerBtn = null; private Button recoid_return; private MediaPlayer mPlayer = null; private String paths; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (mMediaRecorder == null) return; Log.i("aaa", "111"); double ratio = (double) mMediaRecorder.getMaxAmplitude() / BASE; double db = 0;// 分贝 // Log.i("aaa", "111" + ratio); //默认的最大音量是100,可以修改,但其实默认的,在测试过程中就有不错的表现 //你可以传自定义的数字进去,但需要在一定的范围内,比如0-200,就需要在xml文件中配置maxVolume //同时,也可以配置灵敏度sensibility // Log.i("bbb", "111"); if (ratio > 1) db = 20 * Math.log10(ratio); //只要有一个线程,不断调用这个方法,就可以使波形变化 //主要,这个方法必须在ui线程中调用 // Log.i("ccc", "111"); // Log.i("ccc", "111" + db); voiceLineView.setVolume((int) (db)); } }; /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ private TextView tm_text; private CountDownTimer timera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_record); // Init(); recoid_return = (Button) findViewById(R.id.recoid_return); voiceLineView = (VoiceLineView) findViewById(R.id.voicLine); kaishi = (ImageButton) findViewById(R.id.MediaRecordBtn); timer = (Chronometer) findViewById(R.id.timer); tm_text = (TextView) findViewById(R.id.tm_text); recoid_return.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); kaishi.setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onClick(View v) { if (!mStartedFlg) { mPlayerBtn.setVisibility(View.INVISIBLE); timer.setBase(SystemClock.elapsedRealtime());//计时器清零 int hour = (int) ((SystemClock.elapsedRealtime() - timer.getBase()) / 1000 / 60); timer.setFormat("0" + String.valueOf(hour) + ":%s"); timer.setVisibility(View.VISIBLE); tm_text.setVisibility(View.GONE); timer.start(); luyin(); mStartedFlg = true; // kaishi.setText("haha"); kaishi.setBackground(getDrawable(R.mipmap.ic_stop)); } else { // stop // if (mStartedFlg) { // stop(); tingzhi(); mPlayerBtn.setVisibility(View.VISIBLE); timer.stop(); mStartedFlg = false; kaishi.setBackground(getDrawable(R.mipmap.ic_start)); long jiesu_time = SystemClock.elapsedRealtime() - timer.getBase();// 保存这次记录了的时间 // Log.i("", "///" + jiesu_time); timera = new CountDownTimer(jiesu_time, 1000) { @Override public void onTick(long millisUntilFinished) { long myhour = (millisUntilFinished / 1000) / 3600; long myminute = ((millisUntilFinished / 1000) - myhour * 3600) / 60; long mysecond = millisUntilFinished / 1000 - myhour * 3600 - myminute * 60; tm_text.setText(myhour + ":" + myminute + ":" + mysecond); // tm_text.setText((millisUntilFinished / 1000) + ""); } @Override public void onFinish() { tm_text.setEnabled(true); tm_text.setText("播放完毕"); mPlayerBtn.setBackground(getDrawable(R.mipmap.a5)); } }; } // } } }); mPlayerBtn = (ImageButton) this.findViewById(R.id.Play_Btn); mPlayerBtn.setOnClickListener(new Button.OnClickListener() { @TargetApi(Build.VERSION_CODES.LOLLIPOP) @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) @Override public void onClick(View v) { if (!mStartedFlg) { // timer.initTime(12); // timer.reStart(); tm_text.setVisibility(View.VISIBLE); timera.start(); timer.setVisibility(View.GONE); tingzhi(); StartPlaying(); /* long recordingTime = SystemClock.elapsedRealtime() - (timer.getBase()/1000/60);// 保存这次记录了的时间 long base = timer.getBase()/1000/60;*/ /* int hour = (int) ((timer.getBase()) / 1000 / 60); timer.setFormat("0" + String.valueOf(hour) + ":%s");*/ // Log.i("222","aaa"+); // mStartedFlg = true; // mRecorderBtn.setBackground(getDrawable(R.mipmap.ic_start)); mPlayerBtn.setBackground(getDrawable(R.mipmap.a6)); } else { // stop if (mStartedFlg) { // timer.onPause(); timera.cancel(); tingzhi(); StopPlaying(); mPlayerBtn.setBackground(getDrawable(R.mipmap.a5)); } mStartedFlg = false; } // Log.e("test", "________________PLAY______________"); /* mMediaOpt.StopRecording(); mMediaOpt.StartPlaying();*/ } }); Thread thread = new Thread(this); thread.start(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. } private void tingzhi() { if (mMediaRecorder != null) { mMediaRecorder.stop(); mMediaRecorder.reset(); // mMediaRecorder.release(); } // isAlive = false; mMediaRecorder = null; } //停止播放 public void StopPlaying() { if (mPlayer != null) { mPlayer.stop(); mPlayer.release(); } mPlayer = null; } //开始播放 public void StartPlaying() { mPlayer = new MediaPlayer(); try { mPlayer.setDataSource(paths); mPlayer.prepare(); mPlayer.start(); } catch (IOException e) { Log.e("11", "prepare() failed"); } } private void luyin() { String path = getSDPath(); if (path != null) { File dir = new File(path + "/aaaa/audio"); if (!dir.exists()) { dir.mkdirs(); } paths = dir + "/" + uid() + getDate() + ".amr"; mMediaRecorder = new MediaRecorder(); mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mMediaRecorder.setOutputFile(paths); mMediaRecorder.setMaxDuration(1000 * 60 * 10); try { mMediaRecorder.prepare(); } catch (IOException e) { e.printStackTrace(); } mMediaRecorder.start(); } } //生成uuid public UUID uid() { UUID uuid = UUID.randomUUID(); return uuid; } /** * 获取系统时间,保存文件以系统时间戳命名 */ public static String getDate() { Calendar ca = Calendar.getInstance(); int year = ca.get(Calendar.YEAR); // 获取年份 int month = ca.get(Calendar.MONTH); // 获取月份 int day = ca.get(Calendar.DATE); // 获取日 int minute = ca.get(Calendar.MINUTE); // 分 int hour = ca.get(Calendar.HOUR); // 小时 int second = ca.get(Calendar.SECOND); // 秒 String date = "" + year + (month + 1) + day + hour + minute + second; Log.d("222", "date:" + date); return date; } /** * 获取SD path */ public String getSDPath() { File sdDir = null; boolean sdCardExist = Environment.getExternalStorageState() .equals(Environment.MEDIA_MOUNTED); // 判断sd卡是否存在 if (sdCardExist) { sdDir = Environment.getExternalStorageDirectory();// 获取跟目录 return sdDir.toString(); } return null; } @Override public void run() { while (isAlive) { handler.sendEmptyMessage(0); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }