欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android一個(gè)類(lèi)實(shí)現(xiàn)錄音與播放實(shí)例

 更新時(shí)間:2022年02月10日 09:10:41   作者:呂氏春秋i  
大家好,本篇文章主要講的是Android一個(gè)類(lèi)實(shí)現(xiàn)錄音與播放實(shí)例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下

前言

最近混合開(kāi)發(fā)的項(xiàng)目 在做語(yǔ)音識(shí)別時(shí) h5拿不到麥克風(fēng)的權(quán)限
幾經(jīng)周折 開(kāi)發(fā)任務(wù)又落到了原生開(kāi)發(fā)這里

先寫(xiě)一個(gè)demo實(shí)現(xiàn)錄音和播放功能 然后由web端同事調(diào)用交互方法

實(shí)現(xiàn)效果

2

代碼實(shí)現(xiàn)

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

    //語(yǔ)音操作對(duì)象
    private MediaPlayer mPlayer = null;
    private MediaRecorder mRecorder = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestPermission();//請(qǐng)求麥克風(fēng)權(quán)限
        //開(kāi)始錄音
        startRecord = findViewById(R.id.startRecord);
        //綁定監(jiān)聽(tīng)器
        startRecord.setOnClickListener(new startRecordListener());

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

        //開(kāi)始播放
        startPlay = findViewById(R.id.startPlay);
        //綁定監(jiān)聽(tīng)器
        startPlay.setOnClickListener(new startPlayListener());

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

        //設(shè)置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();
    }

    //開(kāi)始錄音
    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="開(kāi)始錄音"
        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="結(jié)束錄音"
        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="開(kāi)始播放"
        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="結(jié)束播放"
        app:layout_constraintTop_toBottomOf="@id/startPlay" />

</androidx.constraintlayout.widget.ConstraintLayout>

靜態(tài)權(quán)限

 <!--錄音和寫(xiě)磁盤(pán)權(quán)限-->
    <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" />

動(dòng)態(tài)權(quán)限

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

總結(jié)

到此這篇關(guān)于Android一個(gè)類(lèi)實(shí)現(xiàn)錄音與播放實(shí)例的文章就介紹到這了,更多相關(guān)Android實(shí)現(xiàn)錄音與播放內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論