Search in sources :

Example 16 with Playable

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();
}
Also used : Context(android.content.Context) Playable(de.danoeh.antennapod.core.util.playback.Playable) LocalPSMP(de.danoeh.antennapod.core.service.playback.LocalPSMP) PlaybackServiceMediaPlayer(de.danoeh.antennapod.core.service.playback.PlaybackServiceMediaPlayer) CountDownLatch(java.util.concurrent.CountDownLatch) AssertionFailedError(junit.framework.AssertionFailedError)

Example 17 with Playable

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();
}
Also used : Context(android.content.Context) Playable(de.danoeh.antennapod.core.util.playback.Playable) LocalPSMP(de.danoeh.antennapod.core.service.playback.LocalPSMP) PlaybackServiceMediaPlayer(de.danoeh.antennapod.core.service.playback.PlaybackServiceMediaPlayer) CountDownLatch(java.util.concurrent.CountDownLatch) AssertionFailedError(junit.framework.AssertionFailedError)

Example 18 with Playable

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();
}
Also used : Context(android.content.Context) Playable(de.danoeh.antennapod.core.util.playback.Playable) LocalPSMP(de.danoeh.antennapod.core.service.playback.LocalPSMP) PlaybackServiceMediaPlayer(de.danoeh.antennapod.core.service.playback.PlaybackServiceMediaPlayer) CountDownLatch(java.util.concurrent.CountDownLatch) AssertionFailedError(junit.framework.AssertionFailedError)

Example 19 with Playable

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();
}
Also used : Context(android.content.Context) Playable(de.danoeh.antennapod.core.util.playback.Playable) PlaybackServiceTaskManager(de.danoeh.antennapod.core.service.playback.PlaybackServiceTaskManager) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 20 with Playable

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);
    }
}
Also used : Playable(de.danoeh.antennapod.core.util.playback.Playable) IOException(java.io.IOException)

Aggregations

Playable (de.danoeh.antennapod.core.util.playback.Playable)40 Context (android.content.Context)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 LocalPSMP (de.danoeh.antennapod.core.service.playback.LocalPSMP)11 PlaybackServiceMediaPlayer (de.danoeh.antennapod.core.service.playback.PlaybackServiceMediaPlayer)11 AssertionFailedError (junit.framework.AssertionFailedError)11 FeedMedia (de.danoeh.antennapod.core.feed.FeedMedia)8 FeedItem (de.danoeh.antennapod.core.feed.FeedItem)7 Intent (android.content.Intent)4 PlaybackServiceTaskManager (de.danoeh.antennapod.core.service.playback.PlaybackServiceTaskManager)4 Timeline (de.danoeh.antennapod.core.util.playback.Timeline)4 SharedPreferences (android.content.SharedPreferences)3 TypedArray (android.content.res.TypedArray)3 IconDrawable (com.joanzapata.iconify.IconDrawable)3 TargetApi (android.annotation.TargetApi)2 Color (android.graphics.Color)2 PixelFormat (android.graphics.PixelFormat)2 Uri (android.net.Uri)2 Build (android.os.Build)2 Bundle (android.os.Bundle)2