上一篇讲到了使用意图录音,这篇文章将使用MediaRecorder类来录音,从而提供更多的灵活性。
效果图:
源代码奉上:
<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="vertical" > <TextView android:id="@+id/statusTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Status" android:gravity="center_horizontal" /> <Button android:id="@+id/button_startRecording" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="开始录音" /> <Button android:id="@+id/button_stopRecording" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停止录音" /> <Button android:id="@+id/button_playRecording" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="播放录音" /> <Button android:id="@+id/button_finish" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="完成" /> </LinearLayout>
package com.multimediademo9mediarecorder; import java.io.File; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener, OnCompletionListener { private TextView statusTextView; private Button button_startRecording, button_stopRecording, button_playRecording, button_finish; private MediaRecorder recorder; private MediaPlayer player; private File audioFile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } /** * 实例化组件 */ private void init() { statusTextView = (TextView) findViewById(R.id.statusTextView); // 当运行Activity时,将statusTextView的文本设置为“Ready”。 statusTextView.setText("Ready"); button_startRecording = (Button) findViewById(R.id.button_startRecording); button_playRecording = (Button) findViewById(R.id.button_playRecording); button_stopRecording = (Button) findViewById(R.id.button_stopRecording); button_finish = (Button) findViewById(R.id.button_finish); button_startRecording.setOnClickListener(this); button_playRecording.setOnClickListener(this); button_stopRecording.setOnClickListener(this); button_finish.setOnClickListener(this); button_playRecording.setEnabled(false); button_stopRecording.setEnabled(false); player = new MediaPlayer(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_startRecording: try { /** * 当点击开始录音按钮时,将构造一个新的MediaRecorder,并调用setAudioSource、 * setOutputFormat和setAudioEncoder方法。 */ recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); /** * 然后在SD卡上创建一个新的File对象,并调用MediaRecorder对象上的setOutputFile方法。 */ File path = new File(Environment.getExternalStorageDirectory() .getAbsoluteFile() + "/files/"); path.mkdir(); audioFile = File.createTempFile("recording", ".3gp", path); recorder.setOutputFile(audioFile.getAbsolutePath()); /** * 调用MediaRecorder上的prepare方法,并开始录制。 */ recorder.prepare(); recorder.start(); /** * 最后更新statusTextView,并且更改那些按钮会被启用或禁用。 */ statusTextView.setText("Recording"); button_playRecording.setEnabled(false); button_stopRecording.setEnabled(true); button_startRecording.setEnabled(false); } catch (Exception e) { e.printStackTrace(); } break; case R.id.button_playRecording: /** * 播放录音,使用MediaPlayer构造的对象player */ player.start(); statusTextView.setText("playing"); button_playRecording.setEnabled(false); button_stopRecording.setEnabled(false); button_startRecording.setEnabled(false); break; case R.id.button_stopRecording: /** * 停止录制,并释放MediaRecorder对象。 */ try { recorder.stop(); recorder.release(); player.setOnCompletionListener(this); player.setDataSource(audioFile.getAbsolutePath()); player.prepare(); statusTextView.setText("Ready to Play!!"); button_playRecording.setEnabled(true); button_stopRecording.setEnabled(false); button_stopRecording.setEnabled(false); } catch (Exception e) { e.printStackTrace(); } break; case R.id.button_finish: finish(); break; default: break; } } @Override public void onCompletion(MediaPlayer mp) { button_playRecording.setEnabled(true); button_startRecording.setEnabled(true); button_stopRecording.setEnabled(false); statusTextView.setText("Ready..."); } }
源代码下载:
Android MediaRecorder录音与播放
时间: 2024-10-18 20:23:40