申请简述:
用户将单击 MainActivity.java 上的开始按钮,该按钮将指向 FirstWord.java。在 FirstWord.java 上,用户可以通过单击记录按钮来记录自己说的一个词。有一个文本转语音实例将输出单词的发音。
MediaRecorder 出现问题。一旦用户单击记录按钮,应用程序就会崩溃。
FirstWord.java
文本到语音实例正常工作,但我发布了所有内容,以防 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);
}
}
日志猫
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
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>
我认为这些是相关文件。如果需要其他文件,请评论。
有任何想法吗?谢谢。