Made sure getDuration() and getCurrentPosition() are not called in an

invalid state
This commit is contained in:
daniel oeh 2012-09-26 12:33:54 +02:00
parent 5bd7ee129e
commit ff364f13f9
4 changed files with 88 additions and 30 deletions

View File

@ -23,6 +23,7 @@ import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator;
import de.danoeh.antennapod.dialog.TimeDialog; import de.danoeh.antennapod.dialog.TimeDialog;
import de.danoeh.antennapod.feed.FeedManager; import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.feed.FeedMedia; import de.danoeh.antennapod.feed.FeedMedia;
import de.danoeh.antennapod.service.PlaybackService;
import de.danoeh.antennapod.storage.DownloadRequestException; import de.danoeh.antennapod.storage.DownloadRequestException;
import de.danoeh.antennapod.util.Converter; import de.danoeh.antennapod.util.Converter;
import de.danoeh.antennapod.util.MediaPlayerError; import de.danoeh.antennapod.util.MediaPlayerError;
@ -309,8 +310,8 @@ public abstract class MediaplayerActivity extends SherlockFragmentActivity
protected void onPositionObserverUpdate() { protected void onPositionObserverUpdate() {
int currentPosition = controller.getPosition(); int currentPosition = controller.getPosition();
int duration = controller.getDuration(); int duration = controller.getDuration();
if (currentPosition != PlaybackController.INVALID_TIME if (currentPosition != PlaybackService.INVALID_TIME
&& duration != PlaybackController.INVALID_TIME) { && duration != PlaybackService.INVALID_TIME) {
controller.getMedia().setPosition(currentPosition); controller.getMedia().setPosition(currentPosition);
txtvPosition.setText(Converter txtvPosition.setText(Converter
.getDurationStringLong(currentPosition)); .getDurationStringLong(currentPosition));

View File

@ -106,6 +106,12 @@ public class PlaybackService extends Service {
public static final int NOTIFICATION_TYPE_BUFFER_START = 5; public static final int NOTIFICATION_TYPE_BUFFER_START = 5;
public static final int NOTIFICATION_TYPE_BUFFER_END = 6; public static final int NOTIFICATION_TYPE_BUFFER_END = 6;
/**
* Returned by getPositionSafe() or getDurationSafe() if the playbackService
* is in an invalid state.
*/
public static final int INVALID_TIME = -1;
/** Is true if service is running. */ /** Is true if service is running. */
public static boolean isRunning = false; public static boolean isRunning = false;
@ -134,7 +140,7 @@ public class PlaybackService extends Service {
private SleepTimer sleepTimer; private SleepTimer sleepTimer;
private Future sleepTimerFuture; private Future sleepTimerFuture;
private Thread chapterLoader; private Thread chapterLoader;
private static final int SCHED_EX_POOL_SIZE = 3; private static final int SCHED_EX_POOL_SIZE = 3;
@ -748,7 +754,8 @@ public class PlaybackService extends Service {
/** Pauses playback and destroys service. Recommended for video playback. */ /** Pauses playback and destroys service. Recommended for video playback. */
public void stop() { public void stop() {
if (AppConfig.DEBUG) Log.d(TAG, "Stopping playback"); if (AppConfig.DEBUG)
Log.d(TAG, "Stopping playback");
player.stop(); player.stop();
stopSelf(); stopSelf();
} }
@ -838,7 +845,10 @@ public class PlaybackService extends Service {
* offset from current position (positive or negative) * offset from current position (positive or negative)
* */ * */
public void seekDelta(int delta) { public void seekDelta(int delta) {
seek(player.getCurrentPosition() + delta); int position = getCurrentPositionSafe();
if (position != INVALID_TIME) {
seek(player.getCurrentPosition() + delta);
}
} }
public void seek(int i) { public void seek(int i) {
@ -860,11 +870,13 @@ public class PlaybackService extends Service {
/** Saves the current position of the media file to the DB */ /** Saves the current position of the media file to the DB */
private synchronized void saveCurrentPosition() { private synchronized void saveCurrentPosition() {
if (AppConfig.DEBUG) int position = getCurrentPositionSafe();
Log.d(TAG, if (position != INVALID_TIME) {
"Saving current position to " + player.getCurrentPosition()); if (AppConfig.DEBUG)
media.setPosition(player.getCurrentPosition()); Log.d(TAG, "Saving current position to " + position);
manager.setFeedMedia(this, media); media.setPosition(position);
manager.setFeedMedia(this, media);
}
} }
private void stopWidgetUpdater() { private void stopWidgetUpdater() {
@ -1133,4 +1145,46 @@ public class PlaybackService extends Service {
postStatusUpdateIntent(); postStatusUpdateIntent();
} }
/**
* call getDuration() on mediaplayer or return INVALID_TIME if player is in
* an invalid state. This method should be used instead of calling
* getDuration() directly to avoid an error.
*/
public int getDurationSafe() {
if (status != null && player != null) {
switch (status) {
case PREPARED:
case PLAYING:
case PAUSED:
case SEEKING:
return player.getDuration();
default:
return INVALID_TIME;
}
} else {
return INVALID_TIME;
}
}
/**
* call getCurrentPosition() on mediaplayer or return INVALID_TIME if player
* is in an invalid state. This method should be used instead of calling
* getCurrentPosition() directly to avoid an error.
*/
public int getCurrentPositionSafe() {
if (status != null && player != null) {
switch (status) {
case PREPARED:
case PLAYING:
case PAUSED:
case SEEKING:
return player.getCurrentPosition();
default:
return INVALID_TIME;
}
} else {
return INVALID_TIME;
}
}
} }

View File

@ -86,14 +86,15 @@ public class PlayerWidgetService extends Service {
views.setOnClickPendingIntent(R.id.layout_left, startMediaplayer); views.setOnClickPendingIntent(R.id.layout_left, startMediaplayer);
if (playbackService != null) { if (playbackService != null) {
FeedMedia media = playbackService.getMedia(); FeedMedia media = playbackService.getMedia();
MediaPlayer player = playbackService.getPlayer();
PlayerStatus status = playbackService.getStatus(); PlayerStatus status = playbackService.getStatus();
views.setTextViewText(R.id.txtvTitle, media.getItem().getTitle()); views.setTextViewText(R.id.txtvTitle, media.getItem().getTitle());
if (status == PlayerStatus.PLAYING) { if (status == PlayerStatus.PLAYING) {
views.setTextViewText(R.id.txtvProgress, String progressString = getProgressString(playbackService);
getProgressString(player)); if (progressString != null) {
views.setTextViewText(R.id.txtvProgress, progressString);
}
views.setImageViewResource(R.id.butPlay, R.drawable.av_pause); views.setImageViewResource(R.id.butPlay, R.drawable.av_pause);
} else { } else {
views.setImageViewResource(R.id.butPlay, R.drawable.av_play); views.setImageViewResource(R.id.butPlay, R.drawable.av_play);
@ -125,10 +126,16 @@ public class PlayerWidgetService extends Service {
return PendingIntent.getBroadcast(this, 0, startingIntent, 0); return PendingIntent.getBroadcast(this, 0, startingIntent, 0);
} }
private String getProgressString(MediaPlayer player) { private String getProgressString(PlaybackService ps) {
int position = ps.getCurrentPositionSafe();
return Converter.getDurationStringLong(player.getCurrentPosition()) int duration = ps.getDurationSafe();
+ " / " + Converter.getDurationStringLong(player.getDuration()); if (position != PlaybackService.INVALID_TIME
&& duration != PlaybackService.INVALID_TIME) {
return Converter.getDurationStringLong(position) + " / "
+ Converter.getDurationStringLong(duration);
} else {
return null;
}
} }
private ServiceConnection mConnection = new ServiceConnection() { private ServiceConnection mConnection = new ServiceConnection() {
@ -148,7 +155,7 @@ public class PlayerWidgetService extends Service {
} }
}; };
private void startViewUpdaterIfNotRunning() { private void startViewUpdaterIfNotRunning() {
if (!isUpdating) { if (!isUpdating) {
ViewUpdater updateThread = new ViewUpdater(this); ViewUpdater updateThread = new ViewUpdater(this);
@ -159,12 +166,12 @@ public class PlayerWidgetService extends Service {
static class ViewUpdater extends Thread { static class ViewUpdater extends Thread {
private static final String THREAD_NAME = "ViewUpdater"; private static final String THREAD_NAME = "ViewUpdater";
private PlayerWidgetService service; private PlayerWidgetService service;
public ViewUpdater(PlayerWidgetService service) { public ViewUpdater(PlayerWidgetService service) {
super(); super();
setName(THREAD_NAME); setName(THREAD_NAME);
this.service = service; this.service = service;
} }
@Override @Override

View File

@ -39,13 +39,8 @@ import de.danoeh.antennapod.service.PlayerStatus;
public abstract class PlaybackController { public abstract class PlaybackController {
private static final String TAG = "PlaybackController"; private static final String TAG = "PlaybackController";
/**
* Returned by getPosition() or getDuration() if the playbackService is in
* an invalid state.
*/
public static final int INVALID_TIME = -1;
static final int DEFAULT_SEEK_DELTA = 30000; static final int DEFAULT_SEEK_DELTA = 30000;
public static final int INVALID_TIME = -1;
private Activity activity; private Activity activity;
@ -533,17 +528,18 @@ public abstract class PlaybackController {
public int getPosition() { public int getPosition() {
if (playbackService != null) { if (playbackService != null) {
return playbackService.getPlayer().getCurrentPosition(); return playbackService.getCurrentPositionSafe();
} else { } else {
return INVALID_TIME; return PlaybackService.INVALID_TIME;
} }
} }
public int getDuration() { public int getDuration() {
if (playbackService != null) { if (playbackService != null) {
return playbackService.getPlayer().getDuration(); return playbackService.getDurationSafe();
} else { } else {
return INVALID_TIME; return PlaybackService.INVALID_TIME;
} }
} }