android媒体框架支持录制音频。可以使用android.media.MediaRecord类来录制音频。本节将介绍如何创建应用程序来录制并播放音频内容。该应用程序的用户界面如图所示:
如图所示该应用程序包含四个按钮:两个用于控制录制过程,另外两个用于开始和停止所录制内容的播放。
示例代码如下:
布局xml:
<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">
<Button
android:id="@+id/beginBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="doClick"
android:text="Begin Recording" />
<Button
android:id="@+id/stopBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="doClick"
android:text="Stop Recording" />
<Button
android:id="@+id/playBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="doClick"
android:text="Play Recording" />
<Button
android:id="@+id/stopPlayingRecording"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="doClick"
android:text="Stop Playing Recording" />
</LinearLayout>
布局文件放置了四个按钮控件。
package com.example.mediarecorder;
import java.io.File;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private MediaPlayer mediaPlayer;
private MediaRecorder recorder;
private String OUTPUT_FILE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OUTPUT_FILE = Environment.getExternalStorageDirectory()+"/recordaudio.3gpp";
System.out.println("存储路径:"+OUTPUT_FILE);
}
public void doClick(View v)
{
switch (v.getId()) {
case R.id.beginBtn:
Toast.makeText(MainActivity.this, "开始录制了", Toast.LENGTH_SHORT).show();
try {
beginRecording();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.stopBtn:
Toast.makeText(MainActivity.this, "停止录制了", Toast.LENGTH_SHORT).show();
try {
stopRecording();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.playBtn:
Toast.makeText(MainActivity.this, "开始播放", Toast.LENGTH_SHORT).show();
try {
playRecording();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.stopPlayingRecording:
Toast.makeText(MainActivity.this, "停止播放", Toast.LENGTH_SHORT).show();
try {
stopPlayingRecording();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
//开始录音
private void beginRecording() throws Exception
{
killMediaRecorder();
File outFile = new File(OUTPUT_FILE);
if(outFile.exists()){
outFile.delete();
}
recorder = new MediaRecorder();
//设置声音源麦克风
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置输出格式
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//设置音频编码
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//设置输出文件
recorder.setOutputFile(OUTPUT_FILE);
recorder.prepare();
recorder.start();
}
//停止录音
private void stopRecording() throws Exception
{
if(recorder != null)
{
recorder.stop();
}
}
//
private void killMediaRecorder() {
if(mediaPlayer != null){
mediaPlayer.release();
}
}
//播放录制音频
private void playRecording() throws Exception
{
killMediaPlayer();
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(OUTPUT_FILE);//设置播放源
mediaPlayer.prepare();
mediaPlayer.start();
}
private void killMediaPlayer() {
if(mediaPlayer != null){
mediaPlayer.release();
}
}
//停止播放音频
private void stopPlayingRecording() throws Exception
{
if(mediaPlayer != null){
mediaPlayer.stop();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
killMediaPlayer();
killMediaRecorder();
}
}
该应用主要使用了两个类,MediaRecorder()用于控制录制音频。MediaPlayer类用于播放录制的的音频文件。