亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android利用MediaRecorder實現(xiàn)錄音功能

 更新時間:2022年03月29日 11:00:28   作者:安了個卓  
這篇文章主要為大家詳細介紹了Android利用MediaRecorder實現(xiàn)錄音功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android利用MediaRecorder實現(xiàn)錄音功能 的具體代碼,供大家參考,具體內(nèi)容如下

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.創(chuàng)建MediaRecorder對象,
3.調(diào)用MediaRecorder的start方法;
4.結束錄音
5.調(diào)用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;
? ? ? ? }

? ? }

具體代碼實現(xiàn):

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>

寫入文件的權限,調(diào)用錄音的權限

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論