标签:android blog http ar io os 使用 sp for
首先要了解andriod开发中andriod多媒体框架包含了什么,它包含了获取和编码多种音频格式的支持,因此你几耍轻松把音频合并到你的应用中,若设备支持,使用MediaRecorder APIs便可以完成录音。
本文将会为你展示一下,如何写一个应用从设备上的microphone获取音频,并进行保存和回放.注意哈Android模拟器,是不具有录音的功能的,不过呢真实的设备一般都具有此功能.
如何进行音频获取
从设备获取音频比回放音频或视频要复杂一点,但是也还算简单:
录音,然后回放录音的例子
下面的例子演示了如何设置,开始以及停止音频获取,以及回放录制的文件.
[java] view plaincopyprint?
11. package com.android.audiorecordtest;
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;
26. import java.io.IOException;
29. public class AudioRecordTest extends Activity
30. {
33. //录音按钮
36. //回放按钮
40. //当录音按钮被click时调用此方法,开始或停止录音
49. //当播放按钮被click时调用此方法,开始或停止播放
61. //设置要播放的文件
64. //播放之
本文来自麦子学院: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 blog http ar io os 使用 sp for
原文地址:http://www.cnblogs.com/maiziedu/p/4172347.html