日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android一個類實現錄音與播放實例_Android

作者:呂氏春秋i ? 更新時間: 2022-04-15 編程語言

前言

最近混合開發的項目 在做語音識別時 h5拿不到麥克風的權限
幾經周折 開發任務又落到了原生開發這里

先寫一個demo實現錄音和播放功能 然后由web端同事調用交互方法

實現效果

2

代碼實現

public class MainActivity extends AppCompatActivity {
    private static final String LOG_TAG = "MainActivity";
    //語音文件保存路徑
    private String FileName = null;
    //界面控件
    private Button startRecord;
    private Button startPlay;
    private Button stopRecord;
    private Button stopPlay;

    //語音操作對象
    private MediaPlayer mPlayer = null;
    private MediaRecorder mRecorder = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestPermission();//請求麥克風權限
        //開始錄音
        startRecord = findViewById(R.id.startRecord);
        //綁定監聽器
        startRecord.setOnClickListener(new startRecordListener());

        //結束錄音
        stopRecord = findViewById(R.id.stopRecord);
        stopRecord.setOnClickListener(new stopRecordListener());

        //開始播放
        startPlay = findViewById(R.id.startPlay);
        //綁定監聽器
        startPlay.setOnClickListener(new startPlayListener());

        //結束播放
        stopPlay = findViewById(R.id.stopPlay);
        stopPlay.setOnClickListener(new stopPlayListener());

        //設置sdcard的路徑
        FileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        FileName += "/test.mp3";
    }

    private void requestPermission() {
        PermissionGen.with(this)
                .addRequestCode(100)
                .permissions(Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.WAKE_LOCK)
                .request();
    }

    //開始錄音
    class startRecordListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setOutputFile(FileName);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            try {
                mRecorder.prepare();
                mRecorder.start();
            } catch (IOException e) {
                Log.e(LOG_TAG, "prepare() failed ---" + e.getMessage());
            }
        }

    }

    //停止錄音
    class stopRecordListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            mRecorder.stop();
            mRecorder.release();
            mRecorder = null;
        }
    }

    //播放錄音
    class startPlayListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            mPlayer = new MediaPlayer();
            try {
                mPlayer.setDataSource(FileName);
                mPlayer.prepare();
                mPlayer.start();
            } catch (IOException e) {
                Log.e(LOG_TAG, "播放失敗");
            }
        }

    }

    //停止播放錄音
    class stopPlayListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            mPlayer.release();
            mPlayer = null;
        }

    }


}

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/startRecord"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="100dp"
        android:text="開始錄音"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/stopRecord"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="結束錄音"
        app:layout_constraintBottom_toTopOf="@id/startPlay"
        app:layout_constraintTop_toBottomOf="@id/startRecord" />

    <Button
        android:id="@+id/startPlay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="開始播放"
        app:layout_constraintBottom_toTopOf="@id/stopPlay"
        app:layout_constraintTop_toBottomOf="@id/stopRecord" />

    <Button
        android:id="@+id/stopPlay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="結束播放"
        app:layout_constraintTop_toBottomOf="@id/startPlay" />

</androidx.constraintlayout.widget.ConstraintLayout>

靜態權限

 <!--錄音和寫磁盤權限-->
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

動態權限

  PermissionGen.with(this)
                .addRequestCode(100)
                .permissions(Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.WAKE_LOCK)
                .request();

總結

原文鏈接:https://blog.csdn.net/Life_s/article/details/122843630

欄目分類
最近更新