Android中录制音频
可能有许多Android中利用网络的录制音频或者视屏;下面是一个简单的录制音频的效果:
这里我们需要建一个XML,然后在里面写代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dot me to start"
android:onClick="onAction"
/>
<Button
android:id="@+id/btnStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dot me to stop"
android:onClick="onAction"
/>
<TextView
android:id="@+id/textTiem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="The currentTime is :0" />
</LinearLayout>
然后再主Activity里面进行主要的操作:
package cn.recoder.audio;
import java.io.IOException;
import java.util.UUID;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Recoder_Audio_12Activity extends Activity {
/** Called when the activity is first created. */
private TextView currentTime;
MediaRecorder mediaRecoder = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
currentTime = (TextView) findViewById(R.id.textTiem);
}
// to get the root
public static String root = Environment.getExternalStorageDirectory()
.getAbsolutePath();
private boolean flag = false;
public void onAction(View v) {
switch (v.getId()) {
case R.id.btnStart:
System.out.println("this is btnStart!");
System.out.println("this is btnStart!");
mediaRecoder = new MediaRecorder();
// to set the outputrecoder
mediaRecoder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecoder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecoder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// to set the root
mediaRecoder.setOutputFile(root + "/"
+ UUID.randomUUID().toString() + ".amr");
// to prepare
try {
mediaRecoder.prepare();
flag = true;
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaRecoder.start();
new Thread(new myThread()).start();
break;
case R.id.btnStop:
System.out.println("this is btnStop!");
flag = false;
mediaRecoder.stop();
mediaRecoder.reset();
mediaRecoder.release();
mediaRecoder = null;
break;
}
}
int time = 0;
class myThread implements Runnable {
@Override
public void run() {
while (flag) {
try {
Thread.sleep(1000);
time++;
//在这里,可能会有人会疑惑,因为我们学的都是不能在线程里面直接更新UI,但是,这里我们却直接用到了
//但是这里大家有注意到没有,我们用的是post方法,这里很特殊。
currentTime.post(new Runnable() {
@Override
public void run() {
System.out.println("this is run");
// TODO Auto-generated method stub
currentTime.setText("the currtTime is :" + time);
}
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
这里我们一定要注意权限。因为这里我们访问了SD卡,同时我们我用到了一些Recoder里面的权限,所以,这里我们要添加两个权限:
<!-- 添加sd卡的写入权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 添加音频录制的权限 -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />