How to use Leanback PlaybackSeekDiskDataProvider to set dash manifest thumbnails

57 Views Asked by At

I am using Leanback PlaybackSeekDiskDataProvider class to show thumbnails preview on the seekbar

class PlaybackSeekDiskDataProvider internal constructor(
    duration: Long,
    interval: Long,
    private val mPathPattern: String?,
    private val context: Context
) :
    PlaybackSeekAsyncDataProvider() {
    private val mPaint: Paint
    override fun doInBackground(task: Any?, index: Int, position: Long): Bitmap? {
        try {
            Thread.sleep(100)
        } catch (ex: InterruptedException) {
            // Thread might be interrupted by cancel() call.
        }
        if (isCancelled(task!!)) {
            return null
        }
        val path = mPathPattern?.let { String.format(it, index + 1) } ?: ""
        return if (mPathPattern != null) {
            GlideApp.with(context)
                .asBitmap()
                .load(mPathPattern)
                .transform(GlideThumbnailTransformation(context, position / 1000))
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .submit().get()
        } else {
            val bmp = Bitmap.createBitmap(256, 144, Bitmap.Config.ARGB_8888)
            val canvas = Canvas(bmp)
            canvas.drawColor(Color.YELLOW)
            canvas.drawText(path, 10f, 80f, mPaint)
            canvas.drawText(Integer.toString(index), 10f, 150f, mPaint)
            bmp
        }
    }

    companion object {
        /**
         * Helper function to set a demo seek provider on PlaybackTransportControlGlue based on
         * duration.
         */
        fun setDemoSeekProvider(glue: PlaybackTransportControlGlue<*>, thumbnails: List<String>, context: Context) {

            if (glue.isPrepared) {
                glue.setSeekProvider(
                    PlaybackSeekDiskDataProvider(
                        glue.duration,
                        8000,
                        thumbnails[0],
                        context
                    )
                )
            } else {
                glue.addPlayerCallback(object : PlaybackGlue.PlayerCallback() {
                    override fun onPreparedStateChanged(glue: PlaybackGlue) {
                        if (glue.isPrepared) {
                            glue.removePlayerCallback(this)
                            val transportControlGlue = glue as PlaybackTransportControlGlue<*>
                            transportControlGlue.seekProvider = PlaybackSeekDiskDataProvider(
                                transportControlGlue.duration,
                                8000,
                                thumbnails[0],
                                context
                            )
                        }
                    }
                })
            }
        }
    }

    init {
        val size = (duration / interval).toInt() + 1
        val pos = LongArray(size)
        for (i in pos.indices) {
            pos[i] = i * duration / pos.size
        }
        seekPositions = pos
        mPaint = Paint()
        mPaint.textSize = 16f
        mPaint.color = Color.BLUE
    }
}

I have the list of big image URLs which I got from my manifest and has thumbnails inside but I can only set one URL, I tried the for loop but it only shows the images from the last URL at the end!

I wonder if there is any sample of this class that is using dash manifest and multiple URLs or if anyone can help me with this issue.

0

There are 0 best solutions below