错误是:
05-06 22:11:15.041: E/AndroidRuntime(15780): FATAL EXCEPTION: main
05-06 22:11:15.041: E/AndroidRuntime(15780): java.lang.IllegalStateException
05-06 22:11:15.041: E/AndroidRuntime(15780): at android.media.MediaPlayer.getCurrentPosition(Native Method)
05-06 22:11:15.041: E/AndroidRuntime(15780): at android.azher.qrcodespeech.SearchActivity$2.onClick(SearchActivity.java:109)
05-06 22:11:15.041: E/AndroidRuntime(15780): at android.view.View.performClick(View.java:4204)
05-06 22:11:15.041: E/AndroidRuntime(15780): at android.view.View$PerformClick.run(View.java:17355)
05-06 22:11:15.041: E/AndroidRuntime(15780): at android.os.Handler.handleCallback(Handler.java:725)
05-06 22:11:15.041: E/AndroidRuntime(15780): at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 22:11:15.041: E/AndroidRuntime(15780): at android.os.Looper.loop(Looper.java:137)
05-06 22:11:15.041: E/AndroidRuntime(15780): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-06 22:11:15.041: E/AndroidRuntime(15780): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 22:11:15.041: E/AndroidRuntime(15780): at java.lang.reflect.Method.invoke(Method.java:511)
05-06 22:11:15.041: E/AndroidRuntime(15780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-06 22:11:15.041: E/AndroidRuntime(15780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-06 22:11:15.041: E/AndroidRuntime(15780): at dalvik.system.NativeStart.main(Native Method)
我收到此错误的代码
(正好在这一行int currentPosition = mp.getCurrentPosition();
):
mp = new MediaPlayer();
try {
mp.prepare();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
////---- play the speech ---------------------------------------------------------
btnPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
File appTmpPath = new File(exStoragePath + "/QRSpeech/sounds/");
appTmpPath.mkdirs();
String tempFilename = bookName.replace(" ", "_")+"_"+pageIndex+".3gpp";
String tempDestFile = appTmpPath.getAbsolutePath() +"/" +tempFilename;
Log.d("path >>>>>>> ", tempDestFile);
try {
mp.reset();
FileInputStream fis = new FileInputStream(tempDestFile);
mp.setDataSource(fis.getFD());
if( pauseTime > 0)
mp.seekTo(pauseTime);
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
mp.stop();
mp.release();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
mp.stop();
mp.release();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
mp.stop();
mp.release();
}
int currentPosition = mp.getCurrentPosition();
int total = mp.getDuration();
sb.setMax(total); // Set the Maximum range of the
sb.setProgress(currentPosition);// set current progress to song's
handler.removeCallbacks(moveSeekBarThread);
handler.postDelayed(moveSeekBarThread, 100); //cal the thread after 100 milliseconds
}
});
答案 0 :(得分:2)
如果您调用Mediaplayer的某个功能,并且MediaPlayer处于错误的状态,则会收到此错误。例如,如果您的MediaPlayer位于Initialized-State
,则无法调用start()
,因此你必须等待,直到你的MediaPlayer在Prepared-State
。在http://developer.android.com/reference/android/media/MediaPlayer.html
在您的试用版块中,您release()
是媒体播放器,而不是start()
它(先没有呼叫prepare()
)。通常你会听PreparedState而不是叫start。
您只能在准备/开始或暂停状态下寻找媒体播放器