package com.example.recordtest; import java.io.IOException; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.view.View.OnTouchListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private String mFileName; private MediaRecorder mRecorder; private MediaPlayer mPlayer; private boolean mStartPlay; private Button start_play; private Button start_record; private boolean isStart=false; public MainActivity(){ mFileName = Environment.getExternalStorageDirectory().getAbsolutePath(); mFileName+="/recordertest.3pg"; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start_record = (Button) findViewById(R.id.start_record); start_play = (Button) findViewById(R.id.start_play); start_record.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "录音时间太短", Toast.LENGTH_SHORT).show(); return; } }); start_record.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { // startRecord(); // start_record.setText("松开结束"); return false; } }); start_record.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_CANCEL: Toast.makeText(getApplicationContext(), "录音时间太短", Toast.LENGTH_SHORT).show(); break; case MotionEvent.ACTION_DOWN: start_record.setText("松开结束"); startRecord(); isStart = true; break; case MotionEvent.ACTION_UP: if (isStart) { start_record.setText("按下说话"); stopRecord(); isStart=false; } break; default: break; } return true; } }); mStartPlay = true; start_play.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { onPlay(mStartPlay); if (mStartPlay) { start_play.setText("停止播放"); }else{ start_play.setText("开始播放"); } mStartPlay=!mStartPlay; } }); } /** * 开始录音 */ private void startRecord(){ mRecorder = new MediaRecorder(); //设置输入源 mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //设置输出格式 mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); //设置输出文件名 mRecorder.setOutputFile(mFileName); //设置编码格式 mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); try { //实例化 mRecorder.prepare(); mRecorder.start(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 停止录音 */ private void stopRecord(){ mRecorder.stop(); mRecorder.release(); mRecorder=null; } /** * 当播放按钮被click时调用这个方法,开始或停止播放 * @param start */ private void onPlay(boolean start){ if(start){ startPlay(); }else{ stopPlay(); } } /** * 开始播放 */ private void startPlay(){ mPlayer = new MediaPlayer(); try { mPlayer.setDataSource(mFileName); mPlayer.prepare(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mPlayer.start(); } private void stopPlay(){ //注意停止与释放顺序不可调换 mPlayer.stop(); mPlayer.release(); mPlayer=null; } }
配置过了:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>两个权限。
布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/start_record" android:id="@+id/start_record"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/start_play" android:id="@+id/start_play" android:layout_below="@id/start_record"/> </RelativeLayout>在快速点击按下说话按钮时会崩溃。求解!!!!
看logcat中抛出的异常才能更快的定位问题。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。