Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

java - E/MediaRecorder: stop called in an invalid state: 4

I am making an audio recorder app when i am recording the audio it does records but while stopping it I am getting this error

E/MediaRecorder: stop called in an invalid state: 4

Here is the main activity code:

package com.example.audiorecorder;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private Button play,stop,record;
    private MediaRecorder myaudioRecorder;
    private String outputFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        play=findViewById(R.id.play);
        stop=findViewById(R.id.stop);
        record=findViewById(R.id.record);
        stop.setEnabled(false);
        play.setEnabled(false);

        outputFile= Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording.3gp";

        myaudioRecorder=new MediaRecorder();
        myaudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myaudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myaudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        myaudioRecorder.setOutputFile(outputFile);

        record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    myaudioRecorder.prepare();
                    myaudioRecorder.start();
                } catch (IllegalStateException  | IOException ioe) {

                }
                record.setEnabled(false);
                stop.setEnabled(true);
                Toast.makeText(getApplicationContext(), "Recording...", Toast.LENGTH_LONG).show();
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    myaudioRecorder.stop();
                    myaudioRecorder.release();
                    myaudioRecorder=null;
                    record.setEnabled(true);
                    stop.setEnabled(false);
                    play.setEnabled(true);
                    Toast.makeText(getApplicationContext(),"Audio Recorded Successfully",Toast.LENGTH_LONG).show();
                } catch (RuntimeException stopException){

                }

            }
        });
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MediaPlayer mediaPlayer=new MediaPlayer();
                try {
                    mediaPlayer.setDataSource(outputFile);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    Toast.makeText(getApplicationContext(),"Playing Audio",Toast.LENGTH_LONG).show();
                } catch (Exception e){

                }
            }
        });
    }
}

My XML code

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="168dp"
        android:text="@string/playButton"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="96dp"
        android:text="@string/stopButton"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="@string/recordButton"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

Here is the log cat:

2021-01-04 13:32:08.817 4775-4775/com.example.audiorecorder I/Choreographer: Skipped 35 frames!  The application may be doing too much work on its main thread.
2021-01-04 13:32:12.157 4775-4775/com.example.audiorecorder E/MediaRecorder: stop called in an invalid state: 4
2021-01-04 13:32:13.540 4775-4775/com.example.audiorecorder E/MediaRecorder: stop called in an invalid state: 4
2021-01-04 13:32:14.341 4775-4775/com.example.audiorecorder E/MediaRecorder: stop called in an invalid state: 4
question from:https://stackoverflow.com/questions/65559725/e-mediarecorder-stop-called-in-an-invalid-state-4

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...