经MediaRecorder和MediaPlayer实现声音记录和回放,代码比较简单,直接附着到代码。
xml文档面对只有四个button不贴。
UI
代码:
VoiceActivity.class
package com.zy.ione; import com.zy.media.UPlayer; import com.zy.media.URecorder; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class VoiceActivity extends Activity { private String path = null; private URecorder recorder; private UPlayer player; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_voice); path = Environment.getExternalStorageDirectory().getAbsolutePath(); path += "/ione.pcm"; recorder = new URecorder(path); player = new UPlayer(path); //開始录音 findViewById(R.id.ione_record_start_btn).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(VoiceActivity.this, "start record", Toast.LENGTH_SHORT).show(); recorder.start(); } }); //停止录音 findViewById(R.id.ione_record_stop_btn).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(VoiceActivity.this, "stop record", Toast.LENGTH_SHORT).show(); recorder.stop(); } }); //開始播放 findViewById(R.id.ione_play_start_btn).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(VoiceActivity.this, "start play", Toast.LENGTH_SHORT).show(); player.start(); } }); //停止播放 findViewById(R.id.ione_play_stop_btn).setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(VoiceActivity.this, "stop play", Toast.LENGTH_SHORT).show(); player.stop(); } }); } }操作接口
package com.zy.media; public interface IVoiceManager { public boolean start(); public boolean stop(); }
录音
package com.zy.media; import java.io.IOException; import android.media.MediaRecorder; import android.util.Log; public class URecorder implements IVoiceManager{ private final String TAG = URecorder.class.getName(); private String path; private MediaRecorder mRecorder; public URecorder(String path){ this.path = path; mRecorder = new MediaRecorder(); } /* * 開始录音 * @return boolean */ @Override public boolean start() { //设置音源为Micphone mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //设置封装格式 mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setOutputFile(path); //设置编码格式 mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { mRecorder.prepare(); } catch (IOException e) { Log.e(TAG, "prepare() failed"); } //录音 mRecorder.start(); return false; } /* * 停止录音 * @return boolean */ @Override public boolean stop() { mRecorder.stop(); mRecorder.release(); mRecorder = null; return false; } }
播放
package com.zy.media; import android.media.MediaPlayer; import android.util.Log; public class UPlayer implements IVoiceManager { private final String TAG = UPlayer.class.getName(); private String path; private MediaPlayer mPlayer; public UPlayer(String path){ this.path = path; mPlayer = new MediaPlayer(); } @Override public boolean start() { try { //设置要播放的文件 mPlayer.setDataSource(path); mPlayer.prepare(); //播放 mPlayer.start(); }catch(Exception e){ Log.e(TAG, "prepare() failed"); } return false; } @Override public boolean stop() { mPlayer.stop(); mPlayer.release(); mPlayer = null; return false; } }
版权声明:本文博主原创文章。博客,未经同意,不得转载。