首页 > 代码库 > Android开发教程 录音和播放
Android开发教程 录音和播放
首先要了解andriod开发中andriod多媒体框架包含了什么,它包含了获取和编码多种音频格式的支持,因此你几耍轻松把音频合并到你的应用中,若设备支持,使用MediaRecorder APIs便可以完成录音。
本文将会为你展示一下,如何写一个应用从设备上的microphone获取音频,并进行保存和回放.注意哈Android模拟器,是不具有录音的功能的,不过呢真实的设备一般都具有此功能.
如何进行音频获取
从设备获取音频比回放音频或视频要复杂一点,但是也还算简单:
- 创建一个android.media.MediaRecorder的新实例.
- 使用MediaRecorder.setAudioSource()设置音频源,一般要使用MediaRecorder.AudioSource.MIC.
- 使用MediaRecorder.setOutputFormat()设置输出文件的格式.
- 使用MediaRecorder.setOutputFile()设置输出文件的名字.
- 使用MediaRecorder.setAudioEncoder()设置音频编码.
- 调用MediaRecorder 实例的MediaRecorder.prepare().
- MediaRecorder.start()开始获取音频.
- 调用MediaRecorder.stop().停止.
- 当你用完MediaRecorder实例后,调用MediaRecorder.release(),就会立即释放资源.
录音,然后回放录音的例子
下面的例子演示了如何设置,开始以及停止音频获取,以及回放录制的文件.
[java] view plaincopyprint?
- /*
- * 如果输出文件被写入外部存储,
- * 本应用需要具有写外部存储的权限,
- * 还要具有录音的权限.这些权限必须
- * 在AndroidManifest.xml 文件中声明,像这样:
- *
- * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- * <uses-permission android:name="android.permission.RECORD_AUDIO" />
- *
- 10. */
11. package com.android.audiorecordtest;
- 12.
13. import android.app.Activity;
14. import android.widget.LinearLayout;
15. import android.os.Bundle;
16. import android.os.Environment;
17. import android.view.ViewGroup;
18. import android.widget.Button;
19. import android.view.View;
20. import android.view.View.OnClickListener;
21. import android.content.Context;
22. import android.util.Log;
23. import android.media.MediaRecorder;
24. import android.media.MediaPlayer;
- 25.
26. import java.io.IOException;
- 27.
- 28.
29. public class AudioRecordTest extends Activity
30. {
- 31. private static final String LOG_TAG = "AudioRecordTest";
- 32. private static String mFileName = null;
33. //录音按钮
- 34. private RecordButton mRecordButton = null;
- 35. private MediaRecorder mRecorder = null;
36. //回放按钮
- 37. private PlayButton mPlayButton = null;
- 38. private MediaPlayer mPlayer = null;
- 39.
40. //当录音按钮被click时调用此方法,开始或停止录音
- 41. private void onRecord(boolean start) {
- 42. if (start) {
- 43. startRecording();
- 44. } else {
- 45. stopRecording();
- 46. }
- 47. }
- 48.
49. //当播放按钮被click时调用此方法,开始或停止播放
- 50. private void onPlay(boolean start) {
- 51. if (start) {
- 52. startPlaying();
- 53. } else {
- 54. stopPlaying();
- 55. }
- 56. }
- 57.
- 58. private void startPlaying() {
- 59. mPlayer = new MediaPlayer();
- 60. try {
61. //设置要播放的文件
- 62. mPlayer.setDataSource(mFileName);
- 63. mPlayer.prepare();
64. //播放之
- 65. mPlayer.start();
- 66. } catch (IOException e) {
- 67. Log.e(LOG_TAG, "prepare() failed");
- 68. }
- 69. }
- 70.
- 71. //停止播放
- 72. private void stopPlaying() {
- 73. mPlayer.release();
- 74. mPlayer = null;
- 75. }
- 76.
- 77. private void startRecording() {
- 78. mRecorder = new MediaRecorder();
- 79. //设置音源为Micphone
- 80. mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
- 81. //设置封装格式
- 82. mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
- 83. mRecorder.setOutputFile(mFileName);
- 84. //设置编码格式
- 85. mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
- 86.
- 87. try {
- 88. mRecorder.prepare();
- 89. } catch (IOException e) {
- 90. Log.e(LOG_TAG, "prepare() failed");
- 91. }
- 92.
- 93. mRecorder.start();
- 94. }
- 95.
- 96. private void stopRecording() {
- 97. mRecorder.stop();
- 98. mRecorder.release();
- 99. mRecorder = null;
- }
- //定义录音按钮
- class RecordButton extends Button {
- boolean mStartRecording = true;
- OnClickListener clicker = new OnClickListener() {
- public void onClick(View v) {
- onRecord(mStartRecording);
- if (mStartRecording) {
- setText("Stop recording");
- } else {
- setText("Start recording");
- }
- mStartRecording = !mStartRecording;
- }
- };
- public RecordButton(Context ctx) {
- super(ctx);
- setText("Start recording");
- setOnClickListener(clicker);
- }
- }
- //定义播放按钮
- class PlayButton extends Button {
- boolean mStartPlaying = true;
- OnClickListener clicker = new OnClickListener() {
- public void onClick(View v) {
- onPlay(mStartPlaying);
- if (mStartPlaying) {
- setText("Stop playing");
- } else {
- setText("Start playing");
- }
- mStartPlaying = !mStartPlaying;
- }
- };
- public PlayButton(Context ctx) {
- super(ctx);
- setText("Start playing");
- setOnClickListener(clicker);
- }
- }
- //构造方法
- public AudioRecordTest() {
- mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
- mFileName += "/audiorecordtest.3gp";
- }
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- //构造界面
- LinearLayout ll = new LinearLayout(this);
- mRecordButton = new RecordButton(this);
- ll.addView(mRecordButton,
- new LinearLayout.LayoutParams(
- ViewGroup.LayoutParams.WRAP_CONTENT,
- ViewGroup.LayoutParams.WRAP_CONTENT,
- 0));
- mPlayButton = new PlayButton(this);
- ll.addView(mPlayButton,
- new LinearLayout.LayoutParams(
- ViewGroup.LayoutParams.WRAP_CONTENT,
- ViewGroup.LayoutParams.WRAP_CONTENT,
- 0));
- setContentView(ll);
- }
- @Override
- public void onPause() {
- super.onPause();
- //Activity暂停时释放录音和播放对象
- if (mRecorder != null) {
- mRecorder.release();
- mRecorder = null;
- }
- if (mPlayer != null) {
- mPlayer.release();
- mPlayer = null;
- }
- }
- }
本文来自麦子学院:http://www.maiziedu.com/ /*
* 如果输出文件被写入外部存储,
* 本应用需要具有写外部存储的权限,
* 还要具有录音的权限.这些权限必须
* 在AndroidManifest.xml 文件中声明,像这样:
*
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
* <uses-permission android:name="android.permission.RECORD_AUDIO" />
*
*/
package com.android.audiorecordtest;
import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import java.io.IOException;
public class AudioRecordTest extends Activity
{
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;
//录音按钮
private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
//回放按钮
private PlayButton mPlayButton = null;
private MediaPlayer mPlayer = null;
//当录音按钮被click时调用此方法,开始或停止录音
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
//当播放按钮被click时调用此方法,开始或停止播放
private void onPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
private void startPlaying() {
mPlayer = new MediaPlayer();
try {
//设置要播放的文件
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
//播放之
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
//停止播放
private void stopPlaying() {
mPlayer.release();
mPlayer = null;
}
private void startRecording() {
mRecorder = new MediaRecorder();
//设置音源为Micphone
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置封装格式
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
//设置编码格式
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
//定义录音按钮
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop recording");
} else {
setText("Start recording");
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start recording");
setOnClickListener(clicker);
}
}
//定义播放按钮
class PlayButton extends Button {
boolean mStartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
};
public PlayButton(Context ctx) {
super(ctx);
setText("Start playing");
setOnClickListener(clicker);
}
}
//构造方法
public AudioRecordTest() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//构造界面
LinearLayout ll = new LinearLayout(this);
mRecordButton = new RecordButton(this);
ll.addView(mRecordButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
mPlayButton = new PlayButton(this);
ll.addView(mPlayButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
setContentView(ll);
}
@Override
public void onPause() {
super.onPause();
//Activity暂停时释放录音和播放对象
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
}
本文来自麦子学院:http://www.maiziedu.com/ /*
Android开发教程 录音和播放