Android简单的利用MediaRecorder进行录音的实例代码
程序员文章站
2023-01-01 23:50:47
复制代码 代码如下:package com.ppmeet; import java.io.ioexception; import android.a...
复制代码 代码如下:
package com.ppmeet;
import java.io.ioexception;
import android.app.activity;
import android.graphics.pixelformat;
import android.media.mediarecorder;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.window;
import android.view.windowmanager;
import android.widget.button;
/**
* class name:testbasicaudio<br>
* class description:basic record audio demo<br>
*
* @version 1.00 2011/12/01
* @author codyy)peijiangping
*/
public class testbasicaudio extends activity {
private button button_start;
private button button_stop;
private mediarecorder recorder;
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
getwindow().setformat(pixelformat.translucent);// 让界面横屏
requestwindowfeature(window.feature_no_title);// 去掉界面标题
getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,
windowmanager.layoutparams.flag_fullscreen);
// 重新设置界面大小
setcontentview(r.layout.main);
init();
}
private void init() {
button_start = (button) this.findviewbyid(r.id.start);
button_stop = (button) this.findviewbyid(r.id.stop);
button_stop.setonclicklistener(new audiolisterner());
button_start.setonclicklistener(new audiolisterner());
}
class audiolisterner implements onclicklistener {
@override
public void onclick(view v) {
if (v == button_start) {
initializeaudio();
}
if (v == button_stop) {
recorder.stop();// 停止刻录
// recorder.reset(); // 重新启动mediarecorder.
recorder.release(); // 刻录完成一定要释放资源
// recorder = null;
}
}
private void initializeaudio() {
recorder = new mediarecorder();// new出mediarecorder对象
recorder.setaudiosource(mediarecorder.audiosource.mic);
// 设置mediarecorder的音频源为麦克风
recorder.setoutputformat(mediarecorder.outputformat.raw_amr);
// 设置mediarecorder录制的音频格式
recorder.setaudioencoder(mediarecorder.audioencoder.amr_nb);
// 设置mediarecorder录制音频的编码为amr.
recorder.setoutputfile("/sdcard/peipei.amr");
// 设置录制好的音频文件保存路径
try {
recorder.prepare();// 准备录制
recorder.start();// 开始录制
} catch (illegalstateexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
}
}
}
androidmainfest.xml
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ppmeet"
android:versioncode="1"
android:versionname="1.0" >
<uses-sdk android:minsdkversion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".testbasicaudio"
android:screenorientation="landscape" >
<intent-filter >
<action android:name="android.intent.action.main" />
<category android:name="android.intent.category.launcher" />
</intent-filter>
</activity>
</application>
<!-- 联网权限 -->
<uses-permission android:name="android.permission.internet" />
<!-- 往sdcard写入数据权限 -->
<uses-permission android:name="android.permission.write_external_storage" />
<!-- 录音权限 -->
<uses-permission android:name="android.permission.record_audio" />
<!-- 在sdcard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.mount_unmount_filesystems" />
</manifest>
推荐阅读