Search in sources :

Example 1 with MediaTrack

use of com.google.android.gms.cast.MediaTrack in project Shuttle by timusus.

the class VideoCastControllerFragment method updateClosedCaptionState.

private void updateClosedCaptionState() {
    int state = VideoCastController.CC_HIDDEN;
    if (mCastManager.isFeatureEnabled(CastConfiguration.FEATURE_CAPTIONS_PREFERENCE) && mSelectedMedia != null && mCastManager.getTracksPreferenceManager().isCaptionEnabled()) {
        List<MediaTrack> tracks = mSelectedMedia.getMediaTracks();
        state = Utils.hasAudioOrTextTrack(tracks) ? VideoCastController.CC_ENABLED : VideoCastController.CC_DISABLED;
    }
    mCastController.setClosedCaptionState(state);
}
Also used : MediaTrack(com.google.android.gms.cast.MediaTrack) Point(android.graphics.Point)

Example 2 with MediaTrack

use of com.google.android.gms.cast.MediaTrack in project Shuttle by timusus.

the class TracksChooserDialog method onCreateDialog.

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    // since dialog doesn't expose its root view at this point (doesn't exist yet), we cannot
    // attach to the unknown eventual parent, so we need to pass null for the rootView parameter
    // of the inflate() method
    @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.custom_tracks_dialog_layout, null);
    setUpView(view);
    builder.setView(view).setPositiveButton(getString(R.string.ccl_ok), new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
            List<MediaTrack> selectedTracks = new ArrayList<>();
            MediaTrack textTrack = mTextAdapter.getSelectedTrack();
            if (textTrack.getId() != TEXT_TRACK_NONE_ID) {
                selectedTracks.add(textTrack);
            }
            MediaTrack audioVideoTrack = mAudioVideoAdapter.getSelectedTrack();
            if (audioVideoTrack != null) {
                selectedTracks.add(audioVideoTrack);
            }
            // video track in this dialog
            if (!mVideoTracks.isEmpty()) {
                boolean foundMatch = false;
                for (MediaTrack videoTrack : mVideoTracks) {
                    for (Long activeTrackId : mCastManager.getActiveTrackIds()) {
                        if (videoTrack.getId() == activeTrackId) {
                            // we found an active video track
                            foundMatch = true;
                            selectedTracks.add(videoTrack);
                            break;
                        }
                    }
                    if (foundMatch) {
                        break;
                    }
                }
            }
            mCastManager.notifyTracksSelectedListeners(selectedTracks);
            TracksChooserDialog.this.getDialog().cancel();
        }
    }).setNegativeButton(R.string.ccl_cancel, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            TracksChooserDialog.this.getDialog().cancel();
        }
    }).setOnCancelListener(new DialogInterface.OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {
            TracksChooserDialog.this.getDialog().cancel();
        }
    });
    return builder.create();
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) TextView(android.widget.TextView) View(android.view.View) ListView(android.widget.ListView) SuppressLint(android.annotation.SuppressLint) MediaTrack(com.google.android.gms.cast.MediaTrack) LayoutInflater(android.view.LayoutInflater) SuppressLint(android.annotation.SuppressLint) ArrayList(java.util.ArrayList) List(java.util.List) NonNull(android.support.annotation.NonNull)

Example 3 with MediaTrack

use of com.google.android.gms.cast.MediaTrack in project Shuttle by timusus.

the class TracksChooserDialog method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    Bundle mediaWrapper = getArguments().getBundle(VideoCastManager.EXTRA_MEDIA);
    mMediaInfo = Utils.bundleToMediaInfo(mediaWrapper);
    mCastManager = VideoCastManager.getInstance();
    mActiveTracks = mCastManager.getActiveTrackIds();
    List<MediaTrack> allTracks = mMediaInfo.getMediaTracks();
    if (allTracks == null || allTracks.isEmpty()) {
        Utils.showToast(getActivity(), R.string.ccl_caption_no_tracks_available);
        dismiss();
    }
}
Also used : MediaTrack(com.google.android.gms.cast.MediaTrack) Bundle(android.os.Bundle)

Example 4 with MediaTrack

use of com.google.android.gms.cast.MediaTrack in project Shuttle by timusus.

the class Utils method bundleToMediaInfo.

/**
     * Builds and returns a {@link MediaInfo} that was wrapped in a {@link Bundle} by
     * <code>mediaInfoToBundle</code>. It is assumed that the type of the {@link MediaInfo} is
     * {@code MediaMetaData.MEDIA_TYPE_MOVIE}
     *
     * @see <code>mediaInfoToBundle()</code>
     */
public static MediaInfo bundleToMediaInfo(Bundle wrapper) {
    if (wrapper == null) {
        return null;
    }
    MediaMetadata metaData = new MediaMetadata(wrapper.getInt(KEY_MEDIA_TYPE));
    metaData.putString(MediaMetadata.KEY_SUBTITLE, wrapper.getString(MediaMetadata.KEY_SUBTITLE));
    metaData.putString(MediaMetadata.KEY_TITLE, wrapper.getString(MediaMetadata.KEY_TITLE));
    metaData.putString(MediaMetadata.KEY_STUDIO, wrapper.getString(MediaMetadata.KEY_STUDIO));
    metaData.putString(MediaMetadata.KEY_ALBUM_ARTIST, wrapper.getString(MediaMetadata.KEY_ALBUM_ARTIST));
    metaData.putString(MediaMetadata.KEY_ALBUM_TITLE, wrapper.getString(MediaMetadata.KEY_ALBUM_TITLE));
    metaData.putString(MediaMetadata.KEY_COMPOSER, wrapper.getString(MediaMetadata.KEY_COMPOSER));
    metaData.putString(MediaMetadata.KEY_SERIES_TITLE, wrapper.getString(MediaMetadata.KEY_SERIES_TITLE));
    metaData.putInt(MediaMetadata.KEY_SEASON_NUMBER, wrapper.getInt(MediaMetadata.KEY_SEASON_NUMBER));
    metaData.putInt(MediaMetadata.KEY_EPISODE_NUMBER, wrapper.getInt(MediaMetadata.KEY_EPISODE_NUMBER));
    long releaseDateMillis = wrapper.getLong(MediaMetadata.KEY_RELEASE_DATE, 0);
    if (releaseDateMillis > 0) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(releaseDateMillis);
        metaData.putDate(MediaMetadata.KEY_RELEASE_DATE, calendar);
    }
    ArrayList<String> images = wrapper.getStringArrayList(KEY_IMAGES);
    if (images != null && !images.isEmpty()) {
        for (String url : images) {
            Uri uri = Uri.parse(url);
            metaData.addImage(new WebImage(uri));
        }
    }
    String customDataStr = wrapper.getString(KEY_CUSTOM_DATA);
    JSONObject customData = null;
    if (!TextUtils.isEmpty(customDataStr)) {
        try {
            customData = new JSONObject(customDataStr);
        } catch (JSONException e) {
            LOGE(TAG, "Failed to deserialize the custom data string: custom data= " + customDataStr);
        }
    }
    List<MediaTrack> mediaTracks = null;
    if (wrapper.getString(KEY_TRACKS_DATA) != null) {
        try {
            JSONArray jsonArray = new JSONArray(wrapper.getString(KEY_TRACKS_DATA));
            mediaTracks = new ArrayList<>();
            if (jsonArray.length() > 0) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObj = (JSONObject) jsonArray.get(i);
                    MediaTrack.Builder builder = new MediaTrack.Builder(jsonObj.getLong(KEY_TRACK_ID), jsonObj.getInt(KEY_TRACK_TYPE));
                    if (jsonObj.has(KEY_TRACK_NAME)) {
                        builder.setName(jsonObj.getString(KEY_TRACK_NAME));
                    }
                    if (jsonObj.has(KEY_TRACK_SUBTYPE)) {
                        builder.setSubtype(jsonObj.getInt(KEY_TRACK_SUBTYPE));
                    }
                    if (jsonObj.has(KEY_TRACK_CONTENT_ID)) {
                        builder.setContentId(jsonObj.getString(KEY_TRACK_CONTENT_ID));
                    }
                    if (jsonObj.has(KEY_TRACK_CONTENT_TYPE)) {
                        builder.setContentType(jsonObj.getString(KEY_TRACK_CONTENT_TYPE));
                    }
                    if (jsonObj.has(KEY_TRACK_LANGUAGE)) {
                        builder.setLanguage(jsonObj.getString(KEY_TRACK_LANGUAGE));
                    }
                    if (jsonObj.has(KEY_TRACKS_DATA)) {
                        builder.setCustomData(new JSONObject(jsonObj.getString(KEY_TRACKS_DATA)));
                    }
                    mediaTracks.add(builder.build());
                }
            }
        } catch (JSONException e) {
            LOGE(TAG, "Failed to build media tracks from the wrapper bundle", e);
        }
    }
    MediaInfo.Builder mediaBuilder = new MediaInfo.Builder(wrapper.getString(KEY_URL)).setStreamType(wrapper.getInt(KEY_STREAM_TYPE)).setContentType(wrapper.getString(KEY_CONTENT_TYPE)).setMetadata(metaData).setCustomData(customData).setMediaTracks(mediaTracks);
    if (wrapper.containsKey(KEY_STREAM_DURATION) && wrapper.getLong(KEY_STREAM_DURATION) >= 0) {
        mediaBuilder.setStreamDuration(wrapper.getLong(KEY_STREAM_DURATION));
    }
    return mediaBuilder.build();
}
Also used : Calendar(java.util.Calendar) JSONArray(org.json.JSONArray) WebImage(com.google.android.gms.common.images.WebImage) JSONException(org.json.JSONException) Uri(android.net.Uri) Point(android.graphics.Point) MediaTrack(com.google.android.gms.cast.MediaTrack) MediaInfo(com.google.android.gms.cast.MediaInfo) JSONObject(org.json.JSONObject) MediaMetadata(com.google.android.gms.cast.MediaMetadata)

Example 5 with MediaTrack

use of com.google.android.gms.cast.MediaTrack in project Shuttle by timusus.

the class Utils method mediaInfoToBundle.

/**
     * Builds and returns a {@link Bundle} which contains a select subset of data in the
     * {@link MediaInfo}. Since {@link MediaInfo} is not {@link Parcelable}, one can use this
     * container bundle to pass around from one activity to another.
     *
     * @see <code>bundleToMediaInfo()</code>
     */
public static Bundle mediaInfoToBundle(MediaInfo info) {
    if (info == null) {
        return null;
    }
    MediaMetadata md = info.getMetadata();
    Bundle wrapper = new Bundle();
    wrapper.putString(MediaMetadata.KEY_TITLE, md.getString(MediaMetadata.KEY_TITLE));
    wrapper.putString(MediaMetadata.KEY_SUBTITLE, md.getString(MediaMetadata.KEY_SUBTITLE));
    wrapper.putString(MediaMetadata.KEY_ALBUM_TITLE, md.getString(MediaMetadata.KEY_ALBUM_TITLE));
    wrapper.putString(MediaMetadata.KEY_ALBUM_ARTIST, md.getString(MediaMetadata.KEY_ALBUM_ARTIST));
    wrapper.putString(MediaMetadata.KEY_COMPOSER, md.getString(MediaMetadata.KEY_COMPOSER));
    wrapper.putString(MediaMetadata.KEY_SERIES_TITLE, md.getString(MediaMetadata.KEY_SERIES_TITLE));
    wrapper.putInt(MediaMetadata.KEY_SEASON_NUMBER, md.getInt(MediaMetadata.KEY_SEASON_NUMBER));
    wrapper.putInt(MediaMetadata.KEY_EPISODE_NUMBER, md.getInt(MediaMetadata.KEY_EPISODE_NUMBER));
    Calendar releaseCalendar = md.getDate(MediaMetadata.KEY_RELEASE_DATE);
    if (releaseCalendar != null) {
        long releaseMillis = releaseCalendar.getTimeInMillis();
        wrapper.putLong(MediaMetadata.KEY_RELEASE_DATE, releaseMillis);
    }
    wrapper.putInt(KEY_MEDIA_TYPE, info.getMetadata().getMediaType());
    wrapper.putString(KEY_URL, info.getContentId());
    wrapper.putString(MediaMetadata.KEY_STUDIO, md.getString(MediaMetadata.KEY_STUDIO));
    wrapper.putString(KEY_CONTENT_TYPE, info.getContentType());
    wrapper.putInt(KEY_STREAM_TYPE, info.getStreamType());
    wrapper.putLong(KEY_STREAM_DURATION, info.getStreamDuration());
    if (!md.getImages().isEmpty()) {
        ArrayList<String> urls = new ArrayList<>();
        for (WebImage img : md.getImages()) {
            urls.add(img.getUrl().toString());
        }
        wrapper.putStringArrayList(KEY_IMAGES, urls);
    }
    JSONObject customData = info.getCustomData();
    if (customData != null) {
        wrapper.putString(KEY_CUSTOM_DATA, customData.toString());
    }
    if (info.getMediaTracks() != null && !info.getMediaTracks().isEmpty()) {
        try {
            JSONArray jsonArray = new JSONArray();
            for (MediaTrack mt : info.getMediaTracks()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put(KEY_TRACK_NAME, mt.getName());
                jsonObject.put(KEY_TRACK_CONTENT_ID, mt.getContentId());
                jsonObject.put(KEY_TRACK_ID, mt.getId());
                jsonObject.put(KEY_TRACK_LANGUAGE, mt.getLanguage());
                jsonObject.put(KEY_TRACK_TYPE, mt.getType());
                jsonObject.put(KEY_TRACK_CONTENT_TYPE, mt.getContentType());
                if (mt.getSubtype() != MediaTrack.SUBTYPE_UNKNOWN) {
                    jsonObject.put(KEY_TRACK_SUBTYPE, mt.getSubtype());
                }
                if (mt.getCustomData() != null) {
                    jsonObject.put(KEY_TRACK_CUSTOM_DATA, mt.getCustomData().toString());
                }
                jsonArray.put(jsonObject);
            }
            wrapper.putString(KEY_TRACKS_DATA, jsonArray.toString());
        } catch (JSONException e) {
            LOGE(TAG, "mediaInfoToBundle(): Failed to convert Tracks data to json", e);
        }
    }
    return wrapper;
}
Also used : MediaTrack(com.google.android.gms.cast.MediaTrack) JSONObject(org.json.JSONObject) Bundle(android.os.Bundle) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) MediaMetadata(com.google.android.gms.cast.MediaMetadata) WebImage(com.google.android.gms.common.images.WebImage) JSONException(org.json.JSONException)

Aggregations

MediaTrack (com.google.android.gms.cast.MediaTrack)6 SuppressLint (android.annotation.SuppressLint)2 Point (android.graphics.Point)2 Bundle (android.os.Bundle)2 MediaMetadata (com.google.android.gms.cast.MediaMetadata)2 WebImage (com.google.android.gms.common.images.WebImage)2 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Uri (android.net.Uri)1 NonNull (android.support.annotation.NonNull)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 MediaInfo (com.google.android.gms.cast.MediaInfo)1