这篇文章将为大家详细讲解有关通过在android项目中使用MediaRecorder实现一个录音功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
MainActivity
package com.centaur.collectvoice;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private final static String TAG = "collectvoice";
MediaRecorder mediaRecorder = new MediaRecorder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 开始按钮
* @param view
* @throws IOException
*/
public void onStart(View view) throws IOException {
Toast.makeText(this, "开始收集", Toast.LENGTH_SHORT).show();
// 第1步:设置音频来源(MIC表示麦克风)
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//第2步:设置音频输出格式(默认的输出格式)
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
//第3步:设置音频编码方式(默认的编码方式)
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
//创建一个临时的音频输出文件
// audioFile = File.createTempFile("record_", ".amr");
if (FileUtils.makeFolder("VOICE")){//一个简单的判断文件夹是不是存在 不存在就创建
String path = Environment.getExternalStorageDirectory().toString() + "/" + "VOICE/";
String filePath =path+"record_.amr";
File file = new File(filePath);
//第4步:指定音频输出文件
mediaRecorder.setOutputFile(file.getAbsolutePath());
//第5步:调用prepare方法
mediaRecorder.prepare();
//第6步:调用start方法开始录音
mediaRecorder.start();
}
}
/**
* 关闭按钮
* @param view
*/
public void onStop(View view) {
Toast.makeText(this, "停止收集", Toast.LENGTH_SHORT).show();
mediaRecorder.stop();
}
}
工具类中用到的方法
public static boolean makeFolder(String folder){
File filefolder = new File(Environment.getExternalStorageDirectory().toString() + "/" + folder);
if(!filefolder.exists()){
filefolder.mkdir();
if(filefolder.exists()){
Log.d(TAG,folder+"创建成功");
}
else {
Log.d(TAG,folder+"创建失败");
}
}
return true;
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.centaur.collectvoice.MainActivity">
<Button
android:onClick="onStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="收集声音" />
<Button
android:onClick="onStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止声音" />
</LinearLayout>
关于通过在android项目中使用MediaRecorder实现一个录音功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。