use of de.danoeh.antennapod.core.util.playback.Playable in project AntennaPod by AntennaPod.
the class PlaybackServiceMediaPlayerTest method testPlayMediaObjectStreamNoStartNoPrepare.
public void testPlayMediaObjectStreamNoStartNoPrepare() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext();
final CountDownLatch countDownLatch = new CountDownLatch(2);
PlaybackServiceMediaPlayer.PSMPCallback callback = new DefaultPSMPCallback() {
@Override
public void statusChanged(LocalPSMP.PSMPInfo newInfo) {
try {
checkPSMPInfo(newInfo);
if (newInfo.playerStatus == PlayerStatus.ERROR)
throw new IllegalStateException("MediaPlayer error");
if (countDownLatch.getCount() == 0) {
fail();
} else if (countDownLatch.getCount() == 2) {
assertEquals(PlayerStatus.INITIALIZING, newInfo.playerStatus);
countDownLatch.countDown();
} else {
assertEquals(PlayerStatus.INITIALIZED, newInfo.playerStatus);
countDownLatch.countDown();
}
} catch (AssertionFailedError e) {
if (assertionError == null)
assertionError = e;
}
}
};
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
Playable p = writeTestPlayable(PLAYABLE_FILE_URL, null);
psmp.playMediaObject(p, true, false, false);
boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
if (assertionError != null)
throw assertionError;
assertTrue(res);
assertTrue(psmp.getPSMPInfo().playerStatus == PlayerStatus.INITIALIZED);
assertFalse(psmp.isStartWhenPrepared());
psmp.shutdown();
}
use of de.danoeh.antennapod.core.util.playback.Playable in project AntennaPod by AntennaPod.
the class PlaybackServiceMediaPlayerTest method testPlayMediaObjectStreamStartPrepare.
public void testPlayMediaObjectStreamStartPrepare() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext();
final CountDownLatch countDownLatch = new CountDownLatch(5);
PlaybackServiceMediaPlayer.PSMPCallback callback = new DefaultPSMPCallback() {
@Override
public void statusChanged(LocalPSMP.PSMPInfo newInfo) {
try {
checkPSMPInfo(newInfo);
if (newInfo.playerStatus == PlayerStatus.ERROR)
throw new IllegalStateException("MediaPlayer error");
if (countDownLatch.getCount() == 0) {
fail();
} else if (countDownLatch.getCount() == 5) {
assertEquals(PlayerStatus.INITIALIZING, newInfo.playerStatus);
} else if (countDownLatch.getCount() == 4) {
assertEquals(PlayerStatus.INITIALIZED, newInfo.playerStatus);
} else if (countDownLatch.getCount() == 3) {
assertEquals(PlayerStatus.PREPARING, newInfo.playerStatus);
} else if (countDownLatch.getCount() == 2) {
assertEquals(PlayerStatus.PREPARED, newInfo.playerStatus);
} else if (countDownLatch.getCount() == 1) {
assertEquals(PlayerStatus.PLAYING, newInfo.playerStatus);
}
countDownLatch.countDown();
} catch (AssertionFailedError e) {
if (assertionError == null)
assertionError = e;
}
}
};
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
Playable p = writeTestPlayable(PLAYABLE_FILE_URL, null);
psmp.playMediaObject(p, true, true, true);
boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
if (assertionError != null)
throw assertionError;
assertTrue(res);
assertTrue(psmp.getPSMPInfo().playerStatus == PlayerStatus.PLAYING);
psmp.shutdown();
}
use of de.danoeh.antennapod.core.util.playback.Playable in project AntennaPod by AntennaPod.
the class PlaybackServiceMediaPlayerTest method testPlayMediaObjectLocalStartPrepare.
public void testPlayMediaObjectLocalStartPrepare() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext();
final CountDownLatch countDownLatch = new CountDownLatch(5);
PlaybackServiceMediaPlayer.PSMPCallback callback = new DefaultPSMPCallback() {
@Override
public void statusChanged(LocalPSMP.PSMPInfo newInfo) {
try {
checkPSMPInfo(newInfo);
if (newInfo.playerStatus == PlayerStatus.ERROR)
throw new IllegalStateException("MediaPlayer error");
if (countDownLatch.getCount() == 0) {
fail();
} else if (countDownLatch.getCount() == 5) {
assertEquals(PlayerStatus.INITIALIZING, newInfo.playerStatus);
} else if (countDownLatch.getCount() == 4) {
assertEquals(PlayerStatus.INITIALIZED, newInfo.playerStatus);
} else if (countDownLatch.getCount() == 3) {
assertEquals(PlayerStatus.PREPARING, newInfo.playerStatus);
} else if (countDownLatch.getCount() == 2) {
assertEquals(PlayerStatus.PREPARED, newInfo.playerStatus);
} else if (countDownLatch.getCount() == 1) {
assertEquals(PlayerStatus.PLAYING, newInfo.playerStatus);
}
} catch (AssertionFailedError e) {
if (assertionError == null)
assertionError = e;
} finally {
countDownLatch.countDown();
}
}
};
PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
Playable p = writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL);
psmp.playMediaObject(p, false, true, true);
boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
if (assertionError != null)
throw assertionError;
assertTrue(res);
assertTrue(psmp.getPSMPInfo().playerStatus == PlayerStatus.PLAYING);
psmp.shutdown();
}
use of de.danoeh.antennapod.core.util.playback.Playable in project AntennaPod by AntennaPod.
the class PlaybackServiceTaskManagerTest method testSetSleepTimer.
public void testSetSleepTimer() throws InterruptedException {
final Context c = getInstrumentation().getTargetContext();
final long TIME = 2000;
final long TIMEOUT = 2 * TIME;
final CountDownLatch countDownLatch = new CountDownLatch(1);
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, new PlaybackServiceTaskManager.PSTMCallback() {
@Override
public void positionSaverTick() {
}
@Override
public void onSleepTimerAlmostExpired() {
}
@Override
public void onSleepTimerExpired() {
if (countDownLatch.getCount() == 0) {
fail();
}
countDownLatch.countDown();
}
@Override
public void onSleepTimerReset() {
}
@Override
public void onWidgetUpdaterTick() {
}
@Override
public void onChapterLoaded(Playable media) {
}
});
pstm.setSleepTimer(TIME, false, false);
countDownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS);
pstm.shutdown();
}
use of de.danoeh.antennapod.core.util.playback.Playable in project AntennaPod by AntennaPod.
the class LocalPSMP method playMediaObject.
/**
* Internal implementation of playMediaObject. This method has an additional parameter that allows the caller to force a media player reset even if
* the given playable parameter is the same object as the currently playing media.
* <p/>
* This method requires the playerLock and is executed on the caller's thread.
*
* @see #playMediaObject(de.danoeh.antennapod.core.util.playback.Playable, boolean, boolean, boolean)
*/
private void playMediaObject(@NonNull final Playable playable, final boolean forceReset, final boolean stream, final boolean startWhenPrepared, final boolean prepareImmediately) {
if (!playerLock.isHeldByCurrentThread()) {
throw new IllegalStateException("method requires playerLock");
}
if (media != null) {
if (!forceReset && media.getIdentifier().equals(playable.getIdentifier()) && playerStatus == PlayerStatus.PLAYING) {
// episode is already playing -> ignore method call
Log.d(TAG, "Method call to playMediaObject was ignored: media file already playing.");
return;
} else {
// stop playback of this episode
if (playerStatus == PlayerStatus.PAUSED || playerStatus == PlayerStatus.PLAYING || playerStatus == PlayerStatus.PREPARED) {
mediaPlayer.stop();
}
// set temporarily to pause in order to update list with current position
if (playerStatus == PlayerStatus.PLAYING) {
callback.onPlaybackPause(media, getPosition());
}
if (!media.getIdentifier().equals(playable.getIdentifier())) {
final Playable oldMedia = media;
executor.submit(() -> callback.onPostPlayback(oldMedia, false, false, true));
}
setPlayerStatus(PlayerStatus.INDETERMINATE, null);
}
}
this.media = playable;
this.stream = stream;
this.mediaType = media.getMediaType();
this.videoSize = null;
createMediaPlayer();
LocalPSMP.this.startWhenPrepared.set(startWhenPrepared);
setPlayerStatus(PlayerStatus.INITIALIZING, media);
try {
media.loadMetadata();
callback.onMediaChanged(false);
if (stream) {
mediaPlayer.setDataSource(media.getStreamUrl());
} else {
mediaPlayer.setDataSource(media.getLocalMediaUrl());
}
setPlayerStatus(PlayerStatus.INITIALIZED, media);
if (prepareImmediately) {
setPlayerStatus(PlayerStatus.PREPARING, media);
mediaPlayer.prepare();
onPrepared(startWhenPrepared);
}
} catch (Playable.PlayableException | IOException | IllegalStateException e) {
e.printStackTrace();
setPlayerStatus(PlayerStatus.ERROR, null);
}
}
Aggregations