本文实例为大家分享了Android利用MediaRecorder实现录音功能 的具体代码,供大家参考,具体内容如下
android用手机录音保存到sd卡中;
布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"? ? xmlns:app="http://schemas.android.com/apk/res-auto"? ? xmlns:tools="http://schemas.android.com/tools"? ? android:layout_width="match_parent"? ? android:layout_height="match_parent"? ? android:orientation="vertical"? ? tools:context=".MainActivity"> ? ?<Button? ? ? ?android:id="@+id/bt_start"? ? ? ?android:layout_width="match_parent"? ? ? ?android:text="start"? ? ? ?android:layout_height="wrap_content"></Button>? ? <Button? ? ? ? android:id="@+id/bt_end"? ? ? ? android:layout_width="match_parent"? ? ? ? android:layout_height="wrap_content"? ? ? ? android:text="stop"></Button> </LinearLayout>
1.准备保存文件的路径及文件;2.创建MediaRecorder对象,3.调用MediaRecorder的start方法;4.结束录音5.调用MediaRecorder的stop方法;6.释放资源;
开始录音:
private void startRecord(){ ? ? ? ? if (recorder==null){? ? ? ? ? ? File dir = new File(Environment.getExternalStorageDirectory(),"sound");? ? ? ? ? ? if (!dir.exists()){? ? ? ? ? ? ? ? dir.mkdir(); ? ? ? ? ? ? }? ? ? ? ? ? File file=new File(dir,System.currentTimeMillis()+".amr");? ? ? ? ? ? if (!file.exists()){? ? ? ? ? ? ? ? try {? ? ? ? ? ? ? ? ? ? file.createNewFile();? ? ? ? ? ? ? ? } catch (IOException e) {? ? ? ? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? ? ? ? } ? ? ? ? ? ? }? ? ? ? ? ? recorder =new MediaRecorder();? ? ? ? ? ? recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//输入源通过话筒录音;? ? ? ? ? ? recorder.setOutputFormat(MediaRecorder.AudioEncoder.AMR_WB);//输出格式? ? ? ? ? ? recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);//音频编码? ? ? ? ? ? recorder.setOutputFile(file.getAbsolutePath());//设置写出文件;? ? ? ? ? ? try {? ? ? ? ? ? ? ? recorder.prepare();? ? ? ? ? ? ? ? recorder.start();? ? ? ? ? ? } catch (IOException e) {? ? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? ? } ? ? ? ? } ? ? }
结束录音:
private void endRecord(){? ? ? ? if (recorder!=null){? ? ? ? ? ? recorder.stop();? ? ? ? ? ? recorder.release();? ? ? ? ? ? recorder=null;? ? ? ? } ? ? }
具体代码实现:
package com.example.record; import android.media.MediaRecorder;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; import java.io.File;import java.io.IOException; public class MainActivity extends AppCompatActivity {? ? private Button bt_1,bt2;? ? private MediaRecorder recorder ; ? ? @Override? ? protected void onCreate(Bundle savedInstanceState) {? ? ? ? super.onCreate(savedInstanceState);? ? ? ? setContentView(R.layout.activity_main);? ? ? ? findViewById(R.id.bt_start).setOnClickListener(new View.OnClickListener() {? ? ? ? ? ? @Override? ? ? ? ? ? public void onClick(View v) {? ? ? ? ? ? ? ? startRecord(); ? ? ? ? ? ? }? ? ? ? }); ? ? ? ? findViewById(R.id.bt_end).setOnClickListener(new View.OnClickListener() {? ? ? ? ? ? @Override? ? ? ? ? ? public void onClick(View v) {? ? ? ? ? ? ? ? endRecord();? ? ? ? ? ? }? ? ? ? });? ? }? ? private void startRecord(){ ? ? ? ? if (recorder==null){? ? ? ? ? ? File dir = new File(Environment.getExternalStorageDirectory(),"sound");? ? ? ? ? ? if (!dir.exists()){? ? ? ? ? ? ? ? dir.mkdir(); ? ? ? ? ? ? }? ? ? ? ? ? File file=new File(dir,System.currentTimeMillis()+".amr");? ? ? ? ? ? if (!file.exists()){? ? ? ? ? ? ? ? try {? ? ? ? ? ? ? ? ? ? file.createNewFile();? ? ? ? ? ? ? ? } catch (IOException e) {? ? ? ? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? ? ? ? } ? ? ? ? ? ? }? ? ? ? ? ? recorder =new MediaRecorder();? ? ? ? ? ? recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//输入源通过话筒录音;? ? ? ? ? ? recorder.setOutputFormat(MediaRecorder.AudioEncoder.AMR_WB);//输出格式? ? ? ? ? ? recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);//音频编码? ? ? ? ? ? recorder.setOutputFile(file.getAbsolutePath());//设置写出文件;? ? ? ? ? ? try {? ? ? ? ? ? ? ? recorder.prepare();? ? ? ? ? ? ? ? recorder.start();? ? ? ? ? ? } catch (IOException e) {? ? ? ? ? ? ? ? e.printStackTrace();? ? ? ? ? ? } ? ? ? ? } ? ? }? ? private void endRecord(){? ? ? ? if (recorder!=null){? ? ? ? ? ? recorder.stop();? ? ? ? ? ? recorder.release();? ? ? ? ? ? recorder=null;? ? ? ? } ? ? }}
最后记得添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
写入文件的权限,调用录音的权限
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728