0
希望有人能幫助我在這裏...的Android的MediaController恢復
後拋出IllegalStateException異常在getCurrentPosition()我有流.mp3
文件的活動。它看起來像這樣:
public class PlayMediaActivity extends Activity implements OnPreparedListener, MediaController.MediaPlayerControl{
private static final String TAG = "AudioPlayer";
private MediaPlayer mediaPlayer;
private MediaController mediaController;
private Handler handler = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_media);
String title = this.getIntent().getStringExtra("Title");
String URL = this.getIntent().getStringExtra("URL");
this.setTitle(title); // Title on top of activity.
((TextView)findViewById(R.id.now_playing_text)).setText(title);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(this);
mediaController = new MediaController(this);
try {
mediaPlayer.setDataSource(URL);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
Log.e(TAG, "Could not open file " + title + " for playback.", e);
}
}
@Override
protected void onStop() {
super.onStop();
mediaController.hide();
mediaPlayer.stop();
mediaPlayer.release();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//the MediaController will hide after 3 seconds - tap the screen to make it appear again
mediaController.show();
return false;
}
//--MediaPlayerControl methods----------------------------------------------------
public void start() {
mediaPlayer.start();
}
public void pause() {
mediaPlayer.pause();
}
public int getDuration() {
return mediaPlayer.getDuration();
}
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
public void seekTo(int i) {
mediaPlayer.seekTo(i);
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public int getBufferPercentage() {
return 0;
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
//--------------------------------------------------------------------------------
public void onPrepared(MediaPlayer mediaPlayer) {
Log.d(TAG, "onPrepared");
mediaController.setMediaPlayer(this);
mediaController.setAnchorView(findViewById(R.id.main_audio_view));
handler.post(new Runnable() {
public void run() {
mediaController.setEnabled(true);
mediaController.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_play_media, menu);
return true;
}
}
這似乎工作的偉大,直到我模擬通過telnet(「GSM通話」),一個電話,中斷音頻。當我切換回我的應用我與呼叫完成後,它拋出一個IllegalStateException
從getCurrentPosition()
:
03-29 13:33:59.896: E/AndroidRuntime(1238): FATAL EXCEPTION: main
03-29 13:33:59.896: E/AndroidRuntime(1238): java.lang.IllegalStateException
03-29 13:33:59.896: E/AndroidRuntime(1238): at android.media.MediaPlayer.getCurrentPosition(Native Method)
03-29 13:33:59.896: E/AndroidRuntime(1238): at com.lcboise.lifechurch.PlayMediaActivity.getCurrentPosition(PlayMediaActivity.java:79)
03-29 13:33:59.896: E/AndroidRuntime(1238): at android.widget.MediaController.setProgress(MediaController.java:381)
03-29 13:33:59.896: E/AndroidRuntime(1238): at android.widget.MediaController.show(MediaController.java:284)
03-29 13:33:59.896: E/AndroidRuntime(1238): at android.widget.MediaController.show(MediaController.java:249)
03-29 13:33:59.896: E/AndroidRuntime(1238): at com.lcboise.lifechurch.PlayMediaActivity.onTouchEvent(PlayMediaActivity.java:61)
03-29 13:33:59.896: E/AndroidRuntime(1238): at android.app.Activity.dispatchTouchEvent(Activity.java:2099)
03-29 13:33:59.896: E/AndroidRuntime(1238): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
03-29 13:33:59.896: E/AndroidRuntime(1238): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
03-29 13:33:59.896: E/AndroidRuntime(1238): at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
03-29 13:33:59.896: E/AndroidRuntime(1238): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 13:33:59.896: E/AndroidRuntime(1238): at android.os.Looper.loop(Looper.java:130)
03-29 13:33:59.896: E/AndroidRuntime(1238): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-29 13:33:59.896: E/AndroidRuntime(1238): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 13:33:59.896: E/AndroidRuntime(1238): at java.lang.reflect.Method.invoke(Method.java:507)
03-29 13:33:59.896: E/AndroidRuntime(1238): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-29 13:33:59.896: E/AndroidRuntime(1238): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-29 13:33:59.896: E/AndroidRuntime(1238): at dalvik.system.NativeStart.main(Native Method)
任何想法我做錯了嗎?是否有一些狀態需要中斷時保存,然後在應用恢復時進行恢復?
你能解決這個錯誤嗎? –
是的。這與通話後恢復狀態有關。我將發佈我的整個解決方案作爲答案,然後如果遇到同樣的問題,可以使用任何您需要的。 –