编程语言
首页 > 编程语言> > Java-Chromecast Sender v2.7.0锁屏图像

Java-Chromecast Sender v2.7.0锁屏图像

作者:互联网

我们如何显示运行v2.7.0的chromecast发送器应用的锁屏插图.我花了大约2天的最好的时间来解决这个问题.

v2.7.0库当前在VideoCastManager.java类中具有以下方法:

private void setBitmapForLockScreen(MediaInfo video) {
    if (video == null || mMediaSessionCompat == null) {
        return;
    }
    Uri imgUrl = null;
    Bitmap bm = null;
    List<WebImage> images = video.getMetadata().getImages();
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
        if (images.size() > 1) {
            imgUrl = images.get(1).getUrl();
        } else if (images.size() == 1) {
            imgUrl = images.get(0).getUrl();
        } else if (mContext != null) {
            // we don't have a url for image so get a placeholder image from resources
            bm = BitmapFactory.decodeResource(mContext.getResources(),
                    R.drawable.album_art_placeholder_large);
        }
    } else if (!images.isEmpty()) {
        imgUrl = images.get(0).getUrl();
    } else {
        // we don't have a url for image so get a placeholder image from resources
        bm = BitmapFactory.decodeResource(mContext.getResources(),
                R.drawable.album_art_placeholder);
    }
    if (bm != null) {
        MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController().getMetadata();
        MediaMetadataCompat.Builder newBuilder = currentMetadata == null
                ? new MediaMetadataCompat.Builder()
                : new MediaMetadataCompat.Builder(currentMetadata);
        mMediaSessionCompat.setMetadata(newBuilder
                .putBitmap(MediaMetadataCompat.METADATA_KEY_ART, bm)
                .build());
    } else {
        if (mLockScreenFetchTask != null) {
            mLockScreenFetchTask.cancel(true);
        }
        mLockScreenFetchTask = new FetchBitmapTask() {
            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (mMediaSessionCompat != null) {
                    MediaMetadataCompat currentMetadata = mMediaSessionCompat.getController()
                            .getMetadata();
                    MediaMetadataCompat.Builder newBuilder = currentMetadata == null
                            ? new MediaMetadataCompat.Builder()
                            : new MediaMetadataCompat.Builder(currentMetadata);
                    mMediaSessionCompat.setMetadata(newBuilder
                            .putBitmap(MediaMetadataCompat.METADATA_KEY_ART, bitmap)
                            .build());
                }
                mLockScreenFetchTask = null;
            }
        };
        mLockScreenFetchTask.execute(imgUrl);
    }
}

我尝试用我自己的自定义图像交换album_art_placeholder_large可绘制对象,没有结果.还尝试通过以下行添加位图:

putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bm)

但没有任何效果.我到底在这里想念什么?需要使用什么MediaMetadataCompat键来显示锁屏插图?
还是我从错误的地方开始?

我尝试过的其他一些链接却无济于事:

Android MediaMetadata image on lockscreen is zoomed

Adding more fields in MediaMetada GoogleCast

截至目前,在线文档非常糟糕,并没有太大帮助.

谢谢!

编辑:经过进一步调查,锁定设备以显示锁屏图像似乎需要15-20分钟.不知道为什么.

编辑2:setupMediaSession方法.

private void setUpMediaSession(final MediaInfo info) {
        if (!isFeatureEnabled(CastConfiguration.FEATURE_LOCKSCREEN)) {
            return;
        }
        if (mMediaSessionCompat == null) {
            ComponentName mediaEventReceiver = new ComponentName(mContext,
                    VideoIntentReceiver.class.getName());
            mMediaSessionCompat = new MediaSessionCompat(mContext, "TAG", mediaEventReceiver,
                    null);
            mMediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
                    | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
            mMediaSessionCompat.setActive(true);
            mMediaSessionCompat.setCallback(new MediaSessionCompat.Callback() {
                @Override
                public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
                    KeyEvent keyEvent = mediaButtonIntent
                            .getParcelableExtra(Intent.EXTRA_KEY_EVENT);
                    if (keyEvent != null && (keyEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PAUSE
                            || keyEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY)) {
                        toggle();
                    }
                    return true;
                }

                @Override
                public void onPlay() {
                    toggle();
                }

                @Override
                public void onPause() {
                    toggle();
                }

                private void toggle() {
                    try {
                        togglePlayback();
                    } catch (CastException | TransientNetworkDisconnectionException |
                        NoConnectionException e) {
                        LOGE(TAG, "MediaSessionCompat.Callback(): Failed to toggle playback", e);
                    }
                }
            });
        }

        mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        PendingIntent pi = getCastControllerPendingIntent();
        if (pi != null) {
            mMediaSessionCompat.setSessionActivity(pi);
        }
        if (info == null) {
            mMediaSessionCompat.setPlaybackState(new PlaybackStateCompat.Builder()
                .setState(PlaybackStateCompat.STATE_NONE, 0, 1.0f).build());
        } else {
            mMediaSessionCompat.setPlaybackState(new PlaybackStateCompat.Builder()
                .setState(PlaybackStateCompat.STATE_PLAYING, 0, 1.0f)
                .setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE).build());
        }

        // Update the media session's image
        updateLockScreenImage(info);

        // update the media session's metadata
        updateMediaSessionMetadata();

        mMediaRouter.setMediaSessionCompat(mMediaSessionCompat);
    }

解决方法:

定义MediaInfo时,您是否定义了“较大”的专辑封面? CCL假定您有一个较小的窗口,设置为index = 0,它将用于通知,小型控制器和媒体路由器控制器对话框,而较大的窗口(MediaInfo中的第二个图像,index = 1)将用于锁屏和全屏遥控器.如果这些图像在那里,CCL将自动使用它们.作为测试,运行CastVideos-android(v2),看看是否适合您.

标签:google-cast,android,java,chromecast
来源: https://codeday.me/bug/20191010/1883507.html