这个相比照相机和录像机要简单多了。因为没有用到摄像头。上代码
一、AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.devin.simpleaudiorecorder">
<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"/>
<application
android:allowBackup="true"
android:icon="@drawable/rec_app_icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
二、布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:background="#7c30d1"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_bottom_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/btn_recordAudio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/rec_audio"
android:text="录音" />
<ImageView
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:src="@drawable/stop_rec"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
三、主程序MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Bind(R.id.btn_recordAudio)
ImageView btnRecordAudio;//录制音频
@Bind(R.id.btn_stop)
ImageView btnStop;//停止录制
private MediaRecorder recorder;//媒体录制器
private File savePath;//保存文件夹
private String saveFileName;//保存文件名
private File saveFile;//保存文件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initView();
}
/**
* 初始化
*/
private void initView() {
btnRecordAudio.setEnabled(true);
btnStop.setEnabled(false);
}
/**
* 鼠标点击事件监听
* @param view
*/
@OnClick({R.id.btn_recordAudio, R.id.btn_stop})
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_recordAudio:
startRecordAudio();//录制音频
break;
case R.id.btn_stop:
stopRecord();//停止录制
break;
default:
break;
}
}
/**
* 停止录制
*/
private void stopRecord() {
if (recorder != null) {
recorder.stop();
Toast.makeText(this, "音频文件保存在" + saveFile.getAbsolutePath(), Toast.LENGTH_SHORT).show();
btnRecordAudio.setEnabled(true);
btnStop.setEnabled(false);
}
}
/**
* 录制音频
*/
private void startRecordAudio() {
savePath = Environment.getExternalStorageDirectory();
saveFileName = "record" + System.currentTimeMillis() + ".amr";
saveFile = new File(savePath, saveFileName);
if (recorder == null) {
recorder = new MediaRecorder();
}
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置音频源
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);//设置输出格式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置音频编码
recorder.setOutputFile(saveFile.getAbsolutePath());//设置输出文件
try {
recorder.prepare();
recorder.start();
Toast.makeText(this, "开始录制音频...", Toast.LENGTH_SHORT).show();
btnRecordAudio.setEnabled(false);
btnStop.setEnabled(true);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(recorder!=null){
recorder.stop();
recorder.release();
recorder=null;
}
}
}
运行效果不错。录完接着来。
照相机、录像机和录音机要用到设备,安全软件会禁止权限,需要手动。代码中请求权限再研究。