Java 在无效状态下调用MediaRecorder start:0

Java 在无效状态下调用MediaRecorder start:0,java,android,android-activity,android-mediarecorder,Java,Android,Android Activity,Android Mediarecorder,申请的简要说明: 用户将单击MainActivity.java上的开始按钮,该按钮将指向FirstWord.java。在FirstWord.java上,用户可以通过单击record按钮记录自己说的单词。有一个将输出单词发音的文本到语音实例 MediaRecorder出现了一些问题。一旦用户单击recordButton,应用程序就会崩溃 FirstWord.java text-to-speech实例工作正常,但我发布了所有内容,以防textToSpeech导致MediaRecorder失败 pac

申请的简要说明:

用户将单击MainActivity.java上的开始按钮,该按钮将指向FirstWord.java。在FirstWord.java上,用户可以通过单击record按钮记录自己说的单词。有一个将输出单词发音的文本到语音实例

MediaRecorder出现了一些问题。一旦用户单击recordButton,应用程序就会崩溃

FirstWord.java

text-to-speech实例工作正常,但我发布了所有内容,以防textToSpeech导致MediaRecorder失败

package com.example.learnwords;

import java.io.IOException;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class FirstWord extends Activity implements OnInitListener {

     private TextToSpeech myTTS;
     private int MY_DATA_CHECK_CODE = 0;

        private static MediaRecorder mediaRecorder = new MediaRecorder();
        private static MediaPlayer mediaPlayer = new MediaPlayer();
        private static String audioFilePath;
        private static Button stopButton;
        private static Button playButton;
        private static Button recordButton;

        private boolean isRecording = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_word);   

        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);

        recordButton = (Button) findViewById(R.id.recordButton);
        playButton = (Button) findViewById(R.id.playButton);
        stopButton = (Button) findViewById(R.id.stopButton);

        if (!hasMicrophone()) //calling hasMicrophone
        {
            stopButton.setEnabled(false);
            playButton.setEnabled(false);
            recordButton.setEnabled(false);
        } else {
            playButton.setEnabled(false);
            stopButton.setEnabled(false);
        }

        audioFilePath = 
             Environment.getExternalStorageDirectory().getAbsolutePath() 
                 + "/myaudio.3gp";  
    }
    protected boolean hasMicrophone() {
        PackageManager pmanager = this.getPackageManager();
        return pmanager.hasSystemFeature(
               PackageManager.FEATURE_MICROPHONE);
    }

    public void recordAudio(View view)throws Exception{
        isRecording = true;
        stopButton.setEnabled(true);
        playButton.setEnabled(false);
        recordButton.setEnabled(false);
        try {
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mediaRecorder.setOutputFile(audioFilePath);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mediaRecorder.prepare();
          } catch (Exception e) {
           e.printStackTrace();
          }
        mediaRecorder.start();
}
    public void stopClicked (View view)
    {   
        stopButton.setEnabled(false);
        playButton.setEnabled(true);

        if (isRecording)
        {   
            recordButton.setEnabled(false);
            mediaRecorder.stop();
            mediaRecorder.release();
            mediaRecorder = null;
            isRecording = false;
        } else {
            mediaPlayer.release();
                mediaPlayer = null;
            recordButton.setEnabled(true);
        }
    }
    public void playAudio (View view) throws IOException
    {
        playButton.setEnabled(false);
        recordButton.setEnabled(false);
        stopButton.setEnabled(true);

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(audioFilePath);
        mediaPlayer.prepare();
        mediaPlayer.start();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void speakWords(String speech) {
        //speak straight away
        myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                //the user has the necessary data - create the TTS
                myTTS = new TextToSpeech(this, this);

            }
            else {
                    //no data - install it now
                Intent installTTSIntent = new Intent();
                installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
        }
    }
        //setup TTS
    public void onInit(int initStatus) {
            //check for successful instantiation
        if (initStatus == TextToSpeech.SUCCESS) {
            if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
                myTTS.setLanguage(Locale.US);
        }
        else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
        }
    }
        public void proClicked(View view) {
            TextView textView = (TextView)findViewById(R.id.textView1);
            String word = textView.getText().toString();
            speakWords(word);

        }
}
LogCat

09-19 11:17:20.229: D/ActivityThread(4603): setTargetHeapUtilization:0.25
09-19 11:17:20.229: D/ActivityThread(4603): setTargetHeapIdealFree:8388608
09-19 11:17:20.229: D/ActivityThread(4603): setTargetHeapConcurrentStart:2097152
09-19 11:17:20.680: I/Adreno200-EGLSUB(4603): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-19 11:17:20.720: E/(4603): <s3dReadConfigFile:75>: Can't open file for reading
09-19 11:17:20.720: E/(4603): <s3dReadConfigFile:75>: Can't open file for reading
09-19 11:17:22.061: V/MediaPlayer(4603): constructor
09-19 11:17:22.061: V/MediaPlayer(4603): setListener
09-19 11:17:22.241: I/Adreno200-EGLSUB(4603): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-19 11:17:24.363: I/TextToSpeech(4603): Sucessfully bound to com.google.android.tts
09-19 11:17:24.373: E/SpannableStringBuilder(4603): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-19 11:17:24.373: E/SpannableStringBuilder(4603): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-19 11:17:24.373: I/TextToSpeech(4603): Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.GoogleTTSService}
09-19 11:17:28.998: W/System.err(4603): java.lang.RuntimeException: setAudioSource failed.
09-19 11:17:29.018: W/System.err(4603):     at android.media.MediaRecorder._setAudioSource(Native Method)
09-19 11:17:29.018: W/System.err(4603):     at android.media.MediaRecorder.setAudioSource(MediaRecorder.java:330)
09-19 11:17:29.018: W/System.err(4603):     at com.example.learnwords.FirstWord.recordAudio(FirstWord.java:75)
09-19 11:17:29.018: W/System.err(4603):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 11:17:29.018: W/System.err(4603):     at java.lang.reflect.Method.invoke(Method.java:511)
09-19 11:17:29.018: W/System.err(4603):     at android.view.View$1.onClick(View.java:3674)
09-19 11:17:29.018: W/System.err(4603):     at android.view.View.performClick(View.java:4203)
09-19 11:17:29.018: W/System.err(4603):     at android.view.View$PerformClick.run(View.java:17189)
09-19 11:17:29.018: W/System.err(4603):     at android.os.Handler.handleCallback(Handler.java:615)
09-19 11:17:29.018: W/System.err(4603):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-19 11:17:29.018: W/System.err(4603):     at android.os.Looper.loop(Looper.java:137)
09-19 11:17:29.018: W/System.err(4603):     at android.app.ActivityThread.main(ActivityThread.java:4950)
09-19 11:17:29.018: W/System.err(4603):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 11:17:29.018: W/System.err(4603):     at java.lang.reflect.Method.invoke(Method.java:511)
09-19 11:17:29.028: W/System.err(4603):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
09-19 11:17:29.028: W/System.err(4603):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
09-19 11:17:29.028: W/System.err(4603):     at dalvik.system.NativeStart.main(Native Method)
09-19 11:17:29.028: E/MediaRecorder(4603): start called in an invalid state: 0
09-19 11:17:29.028: W/dalvikvm(4603): threadid=1: thread exiting with uncaught exception (group=0x4196f438)
09-19 11:17:29.038: E/AndroidRuntime(4603): FATAL EXCEPTION: main
09-19 11:17:29.038: E/AndroidRuntime(4603): java.lang.IllegalStateException: Could not execute method of the activity
09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.view.View$1.onClick(View.java:3679)
09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.view.View.performClick(View.java:4203)
09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.view.View$PerformClick.run(View.java:17189)
09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.os.Handler.handleCallback(Handler.java:615)
09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.os.Looper.loop(Looper.java:137)
09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.app.ActivityThread.main(ActivityThread.java:4950)
09-19 11:17:29.038: E/AndroidRuntime(4603):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 11:17:29.038: E/AndroidRuntime(4603):     at java.lang.reflect.Method.invoke(Method.java:511)
09-19 11:17:29.038: E/AndroidRuntime(4603):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
09-19 11:17:29.038: E/AndroidRuntime(4603):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
09-19 11:17:29.038: E/AndroidRuntime(4603):     at dalvik.system.NativeStart.main(Native Method)
09-19 11:17:29.038: E/AndroidRuntime(4603): Caused by: java.lang.reflect.InvocationTargetException
09-19 11:17:29.038: E/AndroidRuntime(4603):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 11:17:29.038: E/AndroidRuntime(4603):     at java.lang.reflect.Method.invoke(Method.java:511)
09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.view.View$1.onClick(View.java:3674)
09-19 11:17:29.038: E/AndroidRuntime(4603):     ... 11 more
09-19 11:17:29.038: E/AndroidRuntime(4603): Caused by: java.lang.IllegalStateException
09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.media.MediaRecorder.start(Native Method)
09-19 11:17:29.038: E/AndroidRuntime(4603):     at com.example.learnwords.FirstWord.recordAudio(FirstWord.java:83)
09-19 11:17:29.038: E/AndroidRuntime(4603):     ... 14 more
09-19 11:17:20.229:D/ActivityThread(4603):设置目标应用程序利用率:0.25
09-19 11:17:20.229:D/ActivityThread(4603):SetTargetTheApidealFree:8388608
09-19 11:17:20.229:D/ActivityThread(4603):设置目标APCONcurrentStart:2097152
09-19 11:17:20.680:I/Adreno200 EGLSUB(4603)::格式RGBA_8888。
09-19 11:17:20.720:E/(4603)::无法打开文件进行读取
09-19 11:17:20.720:E/(4603)::无法打开文件进行读取
09-19 11:17:22.061:V/MediaPlayer(4603):构造函数
09-19 11:17:22.061:V/MediaPlayer(4603):setListener
09-19 11:17:22.241:I/Adreno200 EGLSUB(4603)::格式RGBA_8888。
09-19 11:17:24.363:I/TextToSpeech(4603):成功绑定到com.google.android.tts
09-19 11:17:24.373:E/SpannableStringBuilder(4603):SPAN_EXCLUSIVE_EXCLUSIVE SPAN的长度不能为零
09-19 11:17:24.373:E/SpannableStringBuilder(4603):SPAN_EXCLUSIVE_EXCLUSIVE SPAN的长度不能为零
09-19 11:17:24.373:I/TextToSpeech(4603):连接到组件信息{com.google.android.tts/com.google.android.tts.googletts服务}
09-19 11:17:28.998:W/System.err(4603):java.lang.RuntimeException:setAudioSource失败。
09-19 11:17:29.018:W/System.err(4603):在android.media.MediaRecorder.\u setAudioSource(本机方法)
09-19 11:17:29.018:W/System.err(4603):在android.media.MediaRecorder.setAudioSource(MediaRecorder.java:330)上
09-19 11:17:29.018:W/System.err(4603):在com.example.learnwords.FirstWord.recordAudio(FirstWord.java:75)
09-19 11:17:29.018:W/System.err(4603):位于java.lang.reflect.Method.invokenactive(本机方法)
09-19 11:17:29.018:W/System.err(4603):位于java.lang.reflect.Method.invoke(Method.java:511)
09-19 11:17:29.018:W/System.err(4603):在android.view.view$1.onClick(view.java:3674)
09-19 11:17:29.018:W/System.err(4603):在android.view.view.performClick(view.java:4203)上
09-19 11:17:29.018:W/System.err(4603):在android.view.view$PerformClick.run(view.java:17189)
09-19 11:17:29.018:W/System.err(4603):位于android.os.Handler.handleCallback(Handler.java:615)
09-19 11:17:29.018:W/System.err(4603):位于android.os.Handler.dispatchMessage(Handler.java:92)
09-19 11:17:29.018:W/System.err(4603):位于android.os.Looper.loop(Looper.java:137)
09-19 11:17:29.018:W/System.err(4603):位于android.app.ActivityThread.main(ActivityThread.java:4950)
09-19 11:17:29.018:W/System.err(4603):位于java.lang.reflect.Method.invokenactive(本机方法)
09-19 11:17:29.018:W/System.err(4603):位于java.lang.reflect.Method.invoke(Method.java:511)
09-19 11:17:29.028:W/System.err(4603):位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
09-19 11:17:29.028:W/System.err(4603):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
09-19 11:17:29.028:W/System.err(4603):在dalvik.System.NativeStart.main(本机方法)
09-19 11:17:29.028:E/MediaRecorder(4603):在无效状态下调用启动:0
09-19 11:17:29.028:W/dalvikvm(4603):threadid=1:线程以未捕获异常退出(组=0x4196f438)
09-19 11:17:29.038:E/AndroidRuntime(4603):致命异常:主
09-19 11:17:29.038:E/AndroidRuntime(4603):java.lang.IllegalStateException:无法执行活动的方法
09-19 11:17:29.038:E/AndroidRuntime(4603):在android.view.view$1.onClick(view.java:3679)
09-19 11:17:29.038:E/AndroidRuntime(4603):在android.view.view.performClick(view.java:4203)上
09-19 11:17:29.038:E/AndroidRuntime(4603):在android.view.view$PerformClick.run(view.java:17189)
09-19 11:17:29.038:E/AndroidRuntime(4603):在android.os.Handler.handleCallback(Handler.java:615)上
09-19 11:17:29.038:E/AndroidRuntime(4603):在android.os.Handler.dispatchMessage(Handler.java:92)上
09-19 11:17:29.038:E/AndroidRuntime(4603):在android.os.Looper.loop(Looper.java:137)
09-19 11:17:29.038:E/AndroidRuntime(4603):位于android.app.ActivityThread.main(ActivityThread.java:4950)
09-19 11:17:29.038:E/AndroidRuntime(4603):位于java.lang.reflect.Method.Invokenactive(本机方法)
09-19 11:17:29.038:E/AndroidRuntime(4603):位于java.lang.reflect.Method.invoke(Method.java:511)
09-19 11:17:29.038:E/AndroidRuntime(4603):位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
09-19 11:17:29.038:E/AndroidRuntime(4603):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
09-19 11:17:29.038:E/AndroidRuntime(4603):在dalvik.system.NativeStart.main(本机方法)
09-19 11:17:29.038:E/AndroidRuntime(4603):由以下原因引起:java.lang.reflect.InvocationTargetException
09-19 11:17:29.038:E/AndroidRuntime(4603):位于java.lang.reflect.Method.Invokenactive(本机方法)
09-19 11:17:29.038:E/AndroidRuntime(4603):位于java.lang.reflect.Method.invoke(Method.java:511)
09-19 11:17:29.038:E/AndroidRuntime(4603):在android.view.view$1.onClick(view.java:3674)
09-19 11:17:29.038:E/AndroidRuntime(4603):。。。还有11个
09-19 11:17:29.038:E/AndroidRuntime(4603):由以下原因引起:java.lang.IllegalStateException
09-19 11:17:29.038:E/AndroidRuntime(4603):在android.media.MediaRecorder.start(本机方法)上
09-19 11:17:29.038:E/AndroidRuntime(4603):在com.example.learnwords.FirstWord.recordAudio(FirstWord.java:83)
09-19 11:17:29.038:E/AndroidRuntime(4603):。。。14多
AndroidManifest.XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.learnwords"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.learnwords.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.learnwords.FirstWord"
            android:label="@string/title_activity_first_word"
            android:parentActivityName="com.example.learnwords.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfirstapp.MainActivity" />
        </activity>
    </application>

</manifest>

<uses-permission 
            android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try
                {startRecording(v);}catch (Exception e){}

            }
        });
        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try
                {stopRecording(v);}catch (Exception e){}

            }
        });