Search in sources :

Example 21 with Playable

use of de.danoeh.antennapod.core.util.playback.Playable in project AntennaPod by AntennaPod.

the class PlaybackService method getNextInQueue.

private Playable getNextInQueue(final Playable currentMedia) {
    if (!(currentMedia instanceof FeedMedia)) {
        Log.d(TAG, "getNextInQueue(), but playable not an instance of FeedMedia, so not proceeding");
        return null;
    }
    if (!ClientConfig.playbackServiceCallbacks.useQueue()) {
        Log.d(TAG, "getNextInQueue(), but queue not in use by this app");
        return null;
    }
    Log.d(TAG, "getNextInQueue()");
    FeedMedia media = (FeedMedia) currentMedia;
    try {
        media.loadMetadata();
    } catch (Playable.PlayableException e) {
        Log.e(TAG, "Unable to load metadata to get next in queue", e);
        return null;
    }
    FeedItem item = media.getItem();
    if (item == null) {
        Log.w(TAG, "getNextInQueue() with FeedMedia object whose FeedItem is null");
        return null;
    }
    FeedItem nextItem;
    try {
        final List<FeedItem> queue = taskManager.getQueue();
        nextItem = DBTasks.getQueueSuccessorOfItem(item.getId(), queue);
    } catch (InterruptedException e) {
        Log.e(TAG, "Error handling the queue in order to retrieve the next item", e);
        return null;
    }
    return (nextItem != null) ? nextItem.getMedia() : null;
}
Also used : FeedItem(de.danoeh.antennapod.core.feed.FeedItem) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) Playable(de.danoeh.antennapod.core.util.playback.Playable)

Example 22 with Playable

use of de.danoeh.antennapod.core.util.playback.Playable in project AntennaPod by AntennaPod.

the class FeedMedia method getMediaItem.

/**
     * Returns a MediaItem representing the FeedMedia object.
     * This is used by the MediaBrowserService
     */
public MediaBrowserCompat.MediaItem getMediaItem() {
    Playable p = this;
    MediaDescriptionCompat description = new MediaDescriptionCompat.Builder().setMediaId(String.valueOf(id)).setTitle(p.getEpisodeTitle()).setDescription(p.getFeedTitle()).setSubtitle(p.getFeedTitle()).build();
    return new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);
}
Also used : Playable(de.danoeh.antennapod.core.util.playback.Playable) MediaDescriptionCompat(android.support.v4.media.MediaDescriptionCompat)

Example 23 with Playable

use of de.danoeh.antennapod.core.util.playback.Playable in project AntennaPod by AntennaPod.

the class CastUtils method getPlayable.

//TODO make unit tests for all the conversion methods
/**
     * Converts {@link MediaInfo} objects into the appropriate implementation of {@link Playable}.
     *
     * Unless <code>searchFeedMedia</code> is set to <code>false</code>, this method should not run
     * on the GUI thread.
     *
     * @param media The {@link MediaInfo} object to be converted.
     * @param searchFeedMedia If set to <code>true</code>, the database will be queried to find a
     *              {@link FeedMedia} instance that matches {@param media}.
     * @return {@link Playable} object in a format proper for casting.
     */
public static Playable getPlayable(MediaInfo media, boolean searchFeedMedia) {
    Log.d(TAG, "getPlayable called with searchFeedMedia=" + searchFeedMedia);
    if (media == null) {
        Log.d(TAG, "MediaInfo object provided is null, not converting to any Playable instance");
        return null;
    }
    MediaMetadata metadata = media.getMetadata();
    int version = metadata.getInt(KEY_FORMAT_VERSION);
    if (version <= 0 || version > MAX_VERSION_FORWARD_COMPATIBILITY) {
        Log.w(TAG, "MediaInfo object obtained from the cast device is not compatible with this" + "version of AntennaPod CastUtils, curVer=" + FORMAT_VERSION_VALUE + ", object version=" + version);
        return null;
    }
    Playable result = null;
    if (searchFeedMedia) {
        long mediaId = metadata.getInt(KEY_MEDIA_ID);
        if (mediaId > 0) {
            FeedMedia fMedia = DBReader.getFeedMedia(mediaId);
            if (fMedia != null) {
                try {
                    fMedia.loadMetadata();
                    if (matches(media, fMedia)) {
                        result = fMedia;
                        Log.d(TAG, "FeedMedia object obtained matches the MediaInfo provided. id=" + mediaId);
                    } else {
                        Log.d(TAG, "FeedMedia object obtained does NOT match the MediaInfo provided. id=" + mediaId);
                    }
                } catch (Playable.PlayableException e) {
                    Log.e(TAG, "Unable to load FeedMedia metadata to compare with MediaInfo", e);
                }
            } else {
                Log.d(TAG, "Unable to find in database a FeedMedia with id=" + mediaId);
            }
        }
        if (result == null) {
            FeedItem feedItem = DBReader.getFeedItem(metadata.getString(KEY_FEED_URL), metadata.getString(KEY_EPISODE_IDENTIFIER));
            if (feedItem != null) {
                result = feedItem.getMedia();
                Log.d(TAG, "Found episode that matches the MediaInfo provided. Using its media, if existing.");
            }
        }
    }
    if (result == null) {
        List<WebImage> imageList = metadata.getImages();
        String imageUrl = null;
        if (!imageList.isEmpty()) {
            imageUrl = imageList.get(0).getUrl().toString();
        }
        result = new RemoteMedia(media.getContentId(), metadata.getString(KEY_EPISODE_IDENTIFIER), metadata.getString(KEY_FEED_URL), metadata.getString(MediaMetadata.KEY_SUBTITLE), metadata.getString(MediaMetadata.KEY_TITLE), metadata.getString(KEY_EPISODE_LINK), metadata.getString(MediaMetadata.KEY_ARTIST), imageUrl, metadata.getString(KEY_FEED_WEBSITE), media.getContentType(), metadata.getDate(MediaMetadata.KEY_RELEASE_DATE).getTime());
        String notes = metadata.getString(KEY_EPISODE_NOTES);
        if (!TextUtils.isEmpty(notes)) {
            ((RemoteMedia) result).setNotes(notes);
        }
        Log.d(TAG, "Converted MediaInfo into RemoteMedia");
    }
    if (result.getDuration() == 0 && media.getStreamDuration() > 0) {
        result.setDuration((int) media.getStreamDuration());
    }
    return result;
}
Also used : FeedItem(de.danoeh.antennapod.core.feed.FeedItem) Playable(de.danoeh.antennapod.core.util.playback.Playable) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) MediaMetadata(com.google.android.gms.cast.MediaMetadata) WebImage(com.google.android.gms.common.images.WebImage)

Example 24 with Playable

use of de.danoeh.antennapod.core.util.playback.Playable in project AntennaPod by AntennaPod.

the class RemotePSMP 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.
     *
     * @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 (!CastUtils.isCastable(playable)) {
        Log.d(TAG, "media provided is not compatible with cast device");
        callback.onMediaPlayerInfo(CAST_ERROR_PRIORITY_HIGH, R.string.cast_not_castable);
        Playable nextPlayable = playable;
        do {
            nextPlayable = callback.getNextInQueue(nextPlayable);
        } while (nextPlayable != null && !CastUtils.isCastable(nextPlayable));
        if (nextPlayable != null) {
            playMediaObject(nextPlayable, forceReset, stream, startWhenPrepared, prepareImmediately);
        }
        return;
    }
    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 {
            // set temporarily to pause in order to update list with current position
            boolean isPlaying = playerStatus == PlayerStatus.PLAYING;
            int position = media.getPosition();
            try {
                isPlaying = castMgr.isRemoteMediaPlaying();
                position = (int) castMgr.getCurrentMediaPosition();
            } catch (TransientNetworkDisconnectionException | NoConnectionException e) {
                Log.e(TAG, "Unable to determine whether media was playing, falling back to stored player status", e);
            }
            if (isPlaying) {
                callback.onPlaybackPause(media, position);
            }
            if (!media.getIdentifier().equals(playable.getIdentifier())) {
                final Playable oldMedia = media;
                callback.onPostPlayback(oldMedia, false, false, true);
            }
            setPlayerStatus(PlayerStatus.INDETERMINATE, null);
        }
    }
    this.media = playable;
    remoteMedia = remoteVersion(playable);
    this.mediaType = media.getMediaType();
    this.startWhenPrepared.set(startWhenPrepared);
    setPlayerStatus(PlayerStatus.INITIALIZING, media);
    try {
        media.loadMetadata();
        callback.onMediaChanged(true);
        setPlayerStatus(PlayerStatus.INITIALIZED, media);
        if (prepareImmediately) {
            prepare();
        }
    } catch (Playable.PlayableException e) {
        Log.e(TAG, "Error while loading media metadata", e);
        setPlayerStatus(PlayerStatus.STOPPED, null);
    }
}
Also used : Playable(de.danoeh.antennapod.core.util.playback.Playable) NoConnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.NoConnectionException) TransientNetworkDisconnectionException(com.google.android.libraries.cast.companionlibrary.cast.exceptions.TransientNetworkDisconnectionException)

Example 25 with Playable

use of de.danoeh.antennapod.core.util.playback.Playable in project AntennaPod by AntennaPod.

the class MediaplayerActivity method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (controller == null) {
        return false;
    }
    Playable media = controller.getMedia();
    if (item.getItemId() == android.R.id.home) {
        Intent intent = new Intent(MediaplayerActivity.this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        return true;
    } else {
        if (media != null) {
            switch(item.getItemId()) {
                case R.id.add_to_favorites_item:
                    if (media instanceof FeedMedia) {
                        FeedItem feedItem = ((FeedMedia) media).getItem();
                        if (feedItem != null) {
                            DBWriter.addFavoriteItem(feedItem);
                            isFavorite = true;
                            invalidateOptionsMenu();
                            Toast.makeText(this, R.string.added_to_favorites, Toast.LENGTH_SHORT).show();
                        }
                    }
                    break;
                case R.id.remove_from_favorites_item:
                    if (media instanceof FeedMedia) {
                        FeedItem feedItem = ((FeedMedia) media).getItem();
                        if (feedItem != null) {
                            DBWriter.removeFavoriteItem(feedItem);
                            isFavorite = false;
                            invalidateOptionsMenu();
                            Toast.makeText(this, R.string.removed_from_favorites, Toast.LENGTH_SHORT).show();
                        }
                    }
                    break;
                case R.id.disable_sleeptimer_item:
                    if (controller.serviceAvailable()) {
                        MaterialDialog.Builder stDialog = new MaterialDialog.Builder(this);
                        stDialog.title(R.string.sleep_timer_label);
                        stDialog.content(getString(R.string.time_left_label) + Converter.getDurationStringLong((int) controller.getSleepTimerTimeLeft()));
                        stDialog.positiveText(R.string.disable_sleeptimer_label);
                        stDialog.negativeText(R.string.cancel_label);
                        stDialog.onPositive((dialog, which) -> {
                            dialog.dismiss();
                            controller.disableSleepTimer();
                        });
                        stDialog.onNegative((dialog, which) -> dialog.dismiss());
                        stDialog.build().show();
                    }
                    break;
                case R.id.set_sleeptimer_item:
                    if (controller.serviceAvailable()) {
                        SleepTimerDialog td = new SleepTimerDialog(this) {

                            @Override
                            public void onTimerSet(long millis, boolean shakeToReset, boolean vibrate) {
                                controller.setSleepTimer(millis, shakeToReset, vibrate);
                            }
                        };
                        td.createNewDialog().show();
                    }
                    break;
                case R.id.audio_controls:
                    MaterialDialog dialog = new MaterialDialog.Builder(this).title(R.string.audio_controls).customView(R.layout.audio_controls, true).neutralText(R.string.close_label).onNeutral((dialog1, which) -> {
                        final SeekBar left = (SeekBar) dialog1.findViewById(R.id.volume_left);
                        final SeekBar right = (SeekBar) dialog1.findViewById(R.id.volume_right);
                        UserPreferences.setVolume(left.getProgress(), right.getProgress());
                    }).show();
                    final SeekBar barPlaybackSpeed = (SeekBar) dialog.findViewById(R.id.playback_speed);
                    final Button butDecSpeed = (Button) dialog.findViewById(R.id.butDecSpeed);
                    butDecSpeed.setOnClickListener(v -> {
                        if (controller != null && controller.canSetPlaybackSpeed()) {
                            barPlaybackSpeed.setProgress(barPlaybackSpeed.getProgress() - 2);
                        } else {
                            VariableSpeedDialog.showGetPluginDialog(this);
                        }
                    });
                    final Button butIncSpeed = (Button) dialog.findViewById(R.id.butIncSpeed);
                    butIncSpeed.setOnClickListener(v -> {
                        if (controller != null && controller.canSetPlaybackSpeed()) {
                            barPlaybackSpeed.setProgress(barPlaybackSpeed.getProgress() + 2);
                        } else {
                            VariableSpeedDialog.showGetPluginDialog(this);
                        }
                    });
                    final TextView txtvPlaybackSpeed = (TextView) dialog.findViewById(R.id.txtvPlaybackSpeed);
                    float currentSpeed = 1.0f;
                    try {
                        currentSpeed = Float.parseFloat(UserPreferences.getPlaybackSpeed());
                    } catch (NumberFormatException e) {
                        Log.e(TAG, Log.getStackTraceString(e));
                        UserPreferences.setPlaybackSpeed(String.valueOf(currentSpeed));
                    }
                    txtvPlaybackSpeed.setText(String.format("%.2fx", currentSpeed));
                    barPlaybackSpeed.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                        @Override
                        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                            if (controller != null && controller.canSetPlaybackSpeed()) {
                                float playbackSpeed = (progress + 10) / 20.0f;
                                controller.setPlaybackSpeed(playbackSpeed);
                                String speedPref = String.format(Locale.US, "%.2f", playbackSpeed);
                                UserPreferences.setPlaybackSpeed(speedPref);
                                String speedStr = String.format("%.2fx", playbackSpeed);
                                txtvPlaybackSpeed.setText(speedStr);
                            } else if (fromUser) {
                                float speed = Float.valueOf(UserPreferences.getPlaybackSpeed());
                                barPlaybackSpeed.post(() -> barPlaybackSpeed.setProgress((int) (20 * speed) - 10));
                            }
                        }

                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {
                            if (controller != null && !controller.canSetPlaybackSpeed()) {
                                VariableSpeedDialog.showGetPluginDialog(MediaplayerActivity.this);
                            }
                        }

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {
                        }
                    });
                    barPlaybackSpeed.setProgress((int) (20 * currentSpeed) - 10);
                    final SeekBar barLeftVolume = (SeekBar) dialog.findViewById(R.id.volume_left);
                    barLeftVolume.setProgress(UserPreferences.getLeftVolumePercentage());
                    final SeekBar barRightVolume = (SeekBar) dialog.findViewById(R.id.volume_right);
                    barRightVolume.setProgress(UserPreferences.getRightVolumePercentage());
                    final CheckBox stereoToMono = (CheckBox) dialog.findViewById(R.id.stereo_to_mono);
                    stereoToMono.setChecked(UserPreferences.stereoToMono());
                    if (controller != null && !controller.canDownmix()) {
                        stereoToMono.setEnabled(false);
                        String sonicOnly = getString(R.string.sonic_only);
                        stereoToMono.setText(stereoToMono.getText() + " [" + sonicOnly + "]");
                    }
                    barLeftVolume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                        @Override
                        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                            controller.setVolume(Converter.getVolumeFromPercentage(progress), Converter.getVolumeFromPercentage(barRightVolume.getProgress()));
                        }

                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {
                        }

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {
                        }
                    });
                    barRightVolume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                        @Override
                        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                            controller.setVolume(Converter.getVolumeFromPercentage(barLeftVolume.getProgress()), Converter.getVolumeFromPercentage(progress));
                        }

                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {
                        }

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {
                        }
                    });
                    stereoToMono.setOnCheckedChangeListener((buttonView, isChecked) -> {
                        UserPreferences.stereoToMono(isChecked);
                        if (controller != null) {
                            controller.setDownmix(isChecked);
                        }
                    });
                    break;
                case R.id.visit_website_item:
                    Uri uri = Uri.parse(media.getWebsiteLink());
                    startActivity(new Intent(Intent.ACTION_VIEW, uri));
                    break;
                case R.id.support_item:
                    if (media instanceof FeedMedia) {
                        DBTasks.flattrItemIfLoggedIn(this, ((FeedMedia) media).getItem());
                    }
                    break;
                case R.id.share_link_item:
                    if (media instanceof FeedMedia) {
                        ShareUtils.shareFeedItemLink(this, ((FeedMedia) media).getItem());
                    }
                    break;
                case R.id.share_download_url_item:
                    if (media instanceof FeedMedia) {
                        ShareUtils.shareFeedItemDownloadLink(this, ((FeedMedia) media).getItem());
                    }
                    break;
                case R.id.share_link_with_position_item:
                    if (media instanceof FeedMedia) {
                        ShareUtils.shareFeedItemLink(this, ((FeedMedia) media).getItem(), true);
                    }
                    break;
                case R.id.share_download_url_with_position_item:
                    if (media instanceof FeedMedia) {
                        ShareUtils.shareFeedItemDownloadLink(this, ((FeedMedia) media).getItem(), true);
                    }
                    break;
                default:
                    return false;
            }
            return true;
        } else {
            return false;
        }
    }
}
Also used : Converter(de.danoeh.antennapod.core.util.Converter) PlaybackController(de.danoeh.antennapod.core.util.playback.PlaybackController) ImageButton(android.widget.ImageButton) Bundle(android.os.Bundle) SleepTimerDialog(de.danoeh.antennapod.dialog.SleepTimerDialog) Uri(android.net.Uri) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) Intent(android.content.Intent) PixelFormat(android.graphics.PixelFormat) TypedArray(android.content.res.TypedArray) MenuItem(android.view.MenuItem) Observable(rx.Observable) UserPreferences(de.danoeh.antennapod.core.preferences.UserPreferences) SeekBar(android.widget.SeekBar) CheckBox(android.widget.CheckBox) MenuInflater(android.view.MenuInflater) Locale(java.util.Locale) ShareUtils(de.danoeh.antennapod.core.util.ShareUtils) Toast(android.widget.Toast) Menu(android.view.Menu) Schedulers(rx.schedulers.Schedulers) View(android.view.View) Button(android.widget.Button) OnSeekBarChangeListener(android.widget.SeekBar.OnSeekBarChangeListener) StorageUtils(de.danoeh.antennapod.core.util.StorageUtils) Playable(de.danoeh.antennapod.core.util.playback.Playable) Build(android.os.Build) TargetApi(android.annotation.TargetApi) Log(android.util.Log) MediaPlayerError(de.danoeh.antennapod.core.util.playback.MediaPlayerError) PlaybackService(de.danoeh.antennapod.core.service.playback.PlaybackService) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) R(de.danoeh.antennapod.R) VariableSpeedDialog(de.danoeh.antennapod.dialog.VariableSpeedDialog) Color(android.graphics.Color) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) FontAwesomeIcons(com.joanzapata.iconify.fonts.FontAwesomeIcons) IconDrawable(com.joanzapata.iconify.IconDrawable) AlertDialog(android.support.v7.app.AlertDialog) TextView(android.widget.TextView) Glide(com.bumptech.glide.Glide) SharedPreferences(android.content.SharedPreferences) DBWriter(de.danoeh.antennapod.core.storage.DBWriter) Flavors(de.danoeh.antennapod.core.util.Flavors) DBReader(de.danoeh.antennapod.core.storage.DBReader) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) DBTasks(de.danoeh.antennapod.core.storage.DBTasks) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) SeekBar(android.widget.SeekBar) Intent(android.content.Intent) OnSeekBarChangeListener(android.widget.SeekBar.OnSeekBarChangeListener) Uri(android.net.Uri) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) Playable(de.danoeh.antennapod.core.util.playback.Playable) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) ImageButton(android.widget.ImageButton) Button(android.widget.Button) CheckBox(android.widget.CheckBox) SleepTimerDialog(de.danoeh.antennapod.dialog.SleepTimerDialog) TextView(android.widget.TextView)

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