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.feed.FeedManager;
import de.danoeh.antennapod.feed.FeedMedia;
import de.danoeh.antennapod.service.PlaybackService;
import de.danoeh.antennapod.storage.DownloadRequestException;
import de.danoeh.antennapod.util.Converter;
import de.danoeh.antennapod.util.MediaPlayerError;
@ -309,8 +310,8 @@ public abstract class MediaplayerActivity extends SherlockFragmentActivity
protected void onPositionObserverUpdate() {
int currentPosition = controller.getPosition();
int duration = controller.getDuration();
if (currentPosition != PlaybackController.INVALID_TIME
&& duration != PlaybackController.INVALID_TIME) {
if (currentPosition != PlaybackService.INVALID_TIME
&& duration != PlaybackService.INVALID_TIME) {
controller.getMedia().setPosition(currentPosition);
txtvPosition.setText(Converter
.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_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. */
public static boolean isRunning = false;
@ -134,7 +140,7 @@ public class PlaybackService extends Service {
private SleepTimer sleepTimer;
private Future sleepTimerFuture;
private Thread chapterLoader;
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. */
public void stop() {
if (AppConfig.DEBUG) Log.d(TAG, "Stopping playback");
if (AppConfig.DEBUG)
Log.d(TAG, "Stopping playback");
player.stop();
stopSelf();
}
@ -838,7 +845,10 @@ public class PlaybackService extends Service {
* offset from current position (positive or negative)
* */
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) {
@ -860,11 +870,13 @@ public class PlaybackService extends Service {
/** Saves the current position of the media file to the DB */
private synchronized void saveCurrentPosition() {
if (AppConfig.DEBUG)
Log.d(TAG,
"Saving current position to " + player.getCurrentPosition());
media.setPosition(player.getCurrentPosition());
manager.setFeedMedia(this, media);
int position = getCurrentPositionSafe();
if (position != INVALID_TIME) {
if (AppConfig.DEBUG)
Log.d(TAG, "Saving current position to " + position);
media.setPosition(position);
manager.setFeedMedia(this, media);
}
}
private void stopWidgetUpdater() {
@ -1133,4 +1145,46 @@ public class PlaybackService extends Service {
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);
if (playbackService != null) {
FeedMedia media = playbackService.getMedia();
MediaPlayer player = playbackService.getPlayer();
PlayerStatus status = playbackService.getStatus();
views.setTextViewText(R.id.txtvTitle, media.getItem().getTitle());
if (status == PlayerStatus.PLAYING) {
views.setTextViewText(R.id.txtvProgress,
getProgressString(player));
String progressString = getProgressString(playbackService);
if (progressString != null) {
views.setTextViewText(R.id.txtvProgress, progressString);
}
views.setImageViewResource(R.id.butPlay, R.drawable.av_pause);
} else {
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);
}
private String getProgressString(MediaPlayer player) {
return Converter.getDurationStringLong(player.getCurrentPosition())
+ " / " + Converter.getDurationStringLong(player.getDuration());
private String getProgressString(PlaybackService ps) {
int position = ps.getCurrentPositionSafe();
int duration = ps.getDurationSafe();
if (position != PlaybackService.INVALID_TIME
&& duration != PlaybackService.INVALID_TIME) {
return Converter.getDurationStringLong(position) + " / "
+ Converter.getDurationStringLong(duration);
} else {
return null;
}
}
private ServiceConnection mConnection = new ServiceConnection() {
@ -148,7 +155,7 @@ public class PlayerWidgetService extends Service {
}
};
private void startViewUpdaterIfNotRunning() {
if (!isUpdating) {
ViewUpdater updateThread = new ViewUpdater(this);
@ -159,12 +166,12 @@ public class PlayerWidgetService extends Service {
static class ViewUpdater extends Thread {
private static final String THREAD_NAME = "ViewUpdater";
private PlayerWidgetService service;
public ViewUpdater(PlayerWidgetService service) {
super();
setName(THREAD_NAME);
this.service = service;
}
@Override

View File

@ -39,13 +39,8 @@ import de.danoeh.antennapod.service.PlayerStatus;
public abstract class 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;
public static final int INVALID_TIME = -1;
private Activity activity;
@ -533,17 +528,18 @@ public abstract class PlaybackController {
public int getPosition() {
if (playbackService != null) {
return playbackService.getPlayer().getCurrentPosition();
return playbackService.getCurrentPositionSafe();
} else {
return INVALID_TIME;
return PlaybackService.INVALID_TIME;
}
}
public int getDuration() {
if (playbackService != null) {
return playbackService.getPlayer().getDuration();
return playbackService.getDurationSafe();
} else {
return INVALID_TIME;
return PlaybackService.INVALID_TIME;
}
}