preface

In the recent mixed development project, h5 failed to get the microphone permission when doing speech recognition. After several twists and turns, the development task fell to the native development

First write a demo to implement recording and playing functions and then call interaction methods by web colleagues

Implementation effect

Code implementation

public class MainActivity extends AppCompatActivity {
    private static final String LOG_TAG = "MainActivity";
    // Path to save the voice file
    private String FileName = null;
    // Interface control
    private Button startRecord;
    private Button startPlay;
    private Button stopRecord;
    private Button stopPlay;

    // Voice manipulation object
    private MediaPlayer mPlayer = null;
    private MediaRecorder mRecorder = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestPermission();// Request microphone permission
        // Start recording
        startRecord = findViewById(R.id.startRecord);
        // Bind the listener
        startRecord.setOnClickListener(new startRecordListener());

        // End the recording
        stopRecord = findViewById(R.id.stopRecord);
        stopRecord.setOnClickListener(new stopRecordListener());

        // Start playing
        startPlay = findViewById(R.id.startPlay);
        // Bind the listener
        startPlay.setOnClickListener(new startPlayListener());

        // End the playback
        stopPlay = findViewById(R.id.stopPlay);
        stopPlay.setOnClickListener(new stopPlayListener());

        // Set the path of sdcard
        FileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        FileName += "/test.mp3";
    }

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

    // Start recording
    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()); }}}// Stop recording
    class stopRecordListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            mRecorder.stop();
            mRecorder.release();
            mRecorder = null; }}// Play the recording
    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, "Playback failed"); }}}// Stop playing the recording
    class stopPlayListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            mPlayer.release();
            mPlayer = null; }}}Copy the code

XML


      
<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="Start recording."
        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="End of the recording."
        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="Start playing"
        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="End of play"
        app:layout_constraintTop_toBottomOf="@id/startPlay" />

</androidx.constraintlayout.widget.ConstraintLayout>
Copy the code

Static permissions

 <! -- Record and write disk permissions -->
    <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" />
Copy the code

Dynamic permissions

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

conclusion

2022~ Never stop learning!!