android - MediaRecorder.stop()崩溃了应用程序

时间:2018-01-12 22:52:54

标签: java android android-mediaplayer media-player

每个人,

我试图录制音频。

单击录制按钮时,音频会完美录制。

我尝试停止记录,并在点击停止按钮后立即崩溃。

您能否帮我修复此代码,以便继续构建我的项目。

以下是代码的一部分:

public class window2 extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
Spinner spinner;//choice between record and Speech-2-text.
Button play,stop,record;//record, stop and replay buttons.
String outputFile;//the record file.
MediaRecorder myAudioRecorder;//the record method.
int reRecord = 0;//reRecord value.
int pathVal = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_window2);

    spinner = findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(this);//upon change between record and S2T.
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor edit  = sharedPreferences.edit();
    if( sharedPreferences.getInt("pathVal",0) == 0)
        edit.putInt("pathVal",1);
    else {
        pathVal = sharedPreferences.getInt("pathVal", pathVal);
        edit.putInt("pathVal",pathVal+1);
    }
    pathVal = sharedPreferences.getInt("pathVal", 0);
    edit.commit();
    //3 buttons for record,stop and replay.
    play = findViewById(R.id.button14);
    stop = findViewById(R.id.button13);
    record = findViewById(R.id.button11);

    //to immune issues that may start if the buttons are enabled.
    stop.setEnabled(false);
    play.setEnabled(false);

    //the file of recording.
    outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+pathVal+".3gp";

    //record settings.
    myAudioRecorder = new MediaRecorder();
    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    myAudioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    myAudioRecorder.setOutputFile(outputFile);

    //record button click.
    record.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            play.setEnabled(false);
            try{
                //if we want to re record.
                if(reRecord == 1)
                {
                    //we build the settings again.
                    myAudioRecorder = new MediaRecorder();
                    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    myAudioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    myAudioRecorder.setOutputFile(outputFile);
                }
                //prepare and start methods.
                myAudioRecorder.prepare();
                myAudioRecorder.start();
            }catch (IllegalStateException ise) {
                //something
            }catch (IOException ioe){
                //something
            }

            record.setEnabled(false);
            stop.setEnabled(true);

            Toast.makeText(window2.this, "Recording...", Toast.LENGTH_SHORT).show();
        }
    });

    //stop button click.
    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            myAudioRecorder.stop();
            myAudioRecorder.reset();
            reRecord = 1;
            myAudioRecorder = null;
            record.setEnabled(true);
            stop.setEnabled(false);
            play.setEnabled(true);
            Toast.makeText(window2.this, "Recorded!", Toast.LENGTH_SHORT).show();
        }
    });

    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MediaPlayer mediaPlayer = new MediaPlayer();
            try {
                mediaPlayer.setDataSource(outputFile);
                mediaPlayer.prepare();
                mediaPlayer.start();

                Toast.makeText(window2.this, "Playing.", Toast.LENGTH_SHORT).show();
            } catch (Exception e){
                //something
            }
        }
    });
}


public void OpenWindow4(View view)
{
    Spinner spinner = findViewById(R.id.spinner3);
    String SpinTxt = spinner.getSelectedItem().toString();
    EditText edit2 = findViewById(R.id.editText);
    String editTextTxt = edit2.getText().toString();
    Intent intent = new Intent(this,window4.class);
    intent.putExtra("THE_TEXT",editTextTxt);
    intent.putExtra("SPIN_CHOICE",SpinTxt);
    startActivity(intent);
}

public void OpenWindow4Speech(View view)
{
    Spinner spinner = findViewById(R.id.spinner4);
    String SpinTxt = spinner.getSelectedItem().toString();
    EditText edit2 = findViewById(R.id.editText2);
    String editTextTxt = edit2.getText().toString();
    Intent intent = new Intent(this,window4.class);
    intent.putExtra("THE_RECORD",outputFile);
    intent.putExtra("SPIN_CHOICE",SpinTxt);
    intent.putExtra("THE_TEXT",editTextTxt);

    startActivity(intent);
}


@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    String item = adapterView.getItemAtPosition(i).toString();
    if (item.equals("S2T"))
    {
        Intent intent = new Intent(this,windows2B.class);
        startActivity(intent);
    }

}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}

}

这里是logcat:

01-13 08:42:27.105 1031-1031/com.example.simplenigal.prealphabuilt V/MediaRecorder: prepare
01-13 08:42:27.105 1031-1031/com.example.simplenigal.prealphabuilt V/MediaRecorder: start
01-13 08:42:27.105 1031-1031/com.example.simplenigal.prealphabuilt E/MediaRecorder: start failed: -38
01-13 08:42:27.757 1031-1038/com.example.simplenigal.prealphabuilt I/art: Do partial code cache collection, code=115KB, data=106KB
01-13 08:42:27.757 1031-1038/com.example.simplenigal.prealphabuilt I/art: After code cache collection, code=115KB, data=106KB
01-13 08:42:27.757 1031-1038/com.example.simplenigal.prealphabuilt I/art: Increasing code cache capacity to 512KB
01-13 08:42:27.759 1031-1038/com.example.simplenigal.prealphabuilt I/art: Compiler allocated 6MB to compile void android.view.ViewRootImpl.performTraversals()
01-13 08:42:31.382 1031-1031/com.example.simplenigal.prealphabuilt I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
01-13 08:42:31.452 1031-1031/com.example.simplenigal.prealphabuilt I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
01-13 08:42:31.462 1031-1031/com.example.simplenigal.prealphabuilt V/MediaRecorder: stop
01-13 08:42:31.462 1031-1031/com.example.simplenigal.prealphabuilt E/MediaRecorder: stop called in an invalid state: 0
01-13 08:42:31.463 1031-1031/com.example.simplenigal.prealphabuilt D/AndroidRuntime: Shutting down VM
01-13 08:42:31.468 1031-1031/com.example.simplenigal.prealphabuilt E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                        Process: com.example.simplenigal.prealphabuilt, PID: 1031
                                                                                 java.lang.IllegalStateException
                                                                                     at android.media.MediaRecorder.native_stop(Native Method)
                                                                                     at android.media.MediaRecorder.stop(MediaRecorder.java:896)
                                                                                     at com.example.simplenigal.prealphabuilt.window2$2.onClick(window2.java:102)
                                                                                     at android.view.View.performClick(View.java:5624)
                                                                                     at android.view.View$PerformClick.run(View.java:22441)
                                                                                     at android.os.Handler.handleCallback(Handler.java:751)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:154)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6316)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
感谢每一位关注和帮助的人。

仍在寻求帮助

0 个答案:

没有答案