标签:
主要代码:
package com.shao.mediarecord;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
public Button btnStartMedia = null;
public Button btnStopMedia = null;
public MediaRecorder mediaRecorder = null;
private OnClickListener onclickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnStartMedia:
startMediaRecord();
break;
case R.id.btnStopMedia:
stopMediaRecord();
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.btnStartMedia = (Button) findViewById(R.id.btnStartMedia);
this.btnStopMedia = (Button) findViewById(R.id.btnStopMedia);
this.btnStartMedia.setOnClickListener(onclickListener);
this.btnStopMedia.setOnClickListener(onclickListener);
}
public void startMediaRecord() {
if(mediaRecorder!=null) {
Toast.makeText(this, "ERR", Toast.LENGTH_LONG).show();
return ;
}
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
File file = new File(Environment.getExternalStorageDirectory(), "bianbian");
if(!file.exists()) {
file.mkdirs();
}
File filePath = new File(file, System.currentTimeMillis()+".amr");
if(filePath!=null) {
try {
filePath.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
mediaRecorder.setOutputFile(filePath.getAbsolutePath());
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void stopMediaRecord() {
this.mediaRecorder.stop();
this.mediaRecorder.release();
this.mediaRecorder = null;
}
}
介绍:打开app出现两个按钮,一个开始录音,一个停止录音。点击了开始录音后,说话,能够把声音输出到文件。
实现介绍:
开始播放声音,主要用到MediaRecorder类,可以看到这个类是可以直接实例化的。setAudioSource是设置声音源、setOutputFormat是设置输出文件格式、setAudioEncoder是设置声音编码。
mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
然后 设置输出文件和路径,调用start播放方法即可。
mediaRecorder.setOutputFile(filePath.getAbsolutePath()); mediaRecorder.prepare(); mediaRecorder.start();
附上布局配置文件
<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" tools:context="com.shao.mediarecord.MainActivity" android:orientation="vertical" > <Button android:id="@+id/btnStartMedia" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="开始录制" /> <Button android:id="@+id/btnStopMedia" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="停止录制" /> </LinearLayout>
最好要说的是,读取设备和存储文件。都要增加相应的权限。
<uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
标签:
原文地址:http://my.oschina.net/0x4ad/blog/375053