Android多媒体之Mediarecorder
package com.example.voicerecord; import java.io.File; import java.io.IOException; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class VoiceActivity extends Activity implements OnClickListener { private Button record; private Button pause; private Button stop; private Button play; private MediaPlayer mp; private MediaRecorder mr; private File file; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_voice); initInBack(); initRecoder(); } //初始化MediaPlayer和MediaRecorder private void initInBack() { try { file = new File(Environment.getExternalStorageDirectory().getCanonicalFile(),"qiaoqiaozh.mp3"); } catch (IOException e1) { e1.printStackTrace(); } if(!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } mr = new MediaRecorder(); mr.setAudioSource(MediaRecorder.AudioSource.MIC); mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mr.setOutputFile(file.getAbsolutePath()); try { mr.prepare(); Log.i("TEST", "mouse"); } catch (IllegalStateException e) { Log.i("TEST1", e.toString()); } catch (IOException e) { Log.i("TEST2", e.toString()); } mp = new MediaPlayer(); } //初始化录音控件 private void initRecoder() { record = (Button)findViewById(R.id.record); pause = (Button)findViewById(R.id.pause); stop = (Button)findViewById(R.id.stop); play = (Button)findViewById(R.id.play); record.setOnClickListener(this); pause.setOnClickListener(this); stop.setOnClickListener(this); play.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.voice, menu); return true; } @Override public void onClick(View v) { switch(v.getId()) { case R.id.record: mr.start(); break; case R.id.pause: if(mp.isPlaying()) mp.pause(); case R.id.stop: mr.stop(); break; case R.id.play: try { mp.setDataSource(file.getAbsolutePath()); mp.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mp.start(); break; } } @Override protected void onDestroy() { mr.stop(); mr.release(); mp.stop(); mp.release(); super.onDestroy(); } }
<LinearLayout 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" android:orientation="horizontal" > <Button android:id="@+id/record" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="record" android:layout_weight="1" /> <Button android:id="@+id/pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="pause" android:layout_weight="1" /> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="stop" android:layout_weight="1" /> <Button android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="play" android:layout_weight="1" /> </LinearLayout>
使用MediaRecorder录音并使用MediaPlayer播放。