希望有人可以在这里帮助我...
我有一个流.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
from 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)
任何想法我做错了什么?是否有一些状态我必须在中断时保存,然后在应用程序恢复时恢复?