如何在ExoPlayer for AndroidTV中实现视频清理?

时间:2019-04-22 18:30:38

标签: exoplayer android-tv

我真的很喜欢这个。

我正在阅读Google的这篇文章:https://developer.android.com/training/tv/playback/transport-controls#java

他们说我应该使用此类使一切变为可能: https://developer.android.com/reference/androidx/leanback/widget/PlaybackSeekDataProvider.html

我也在这里寻找示例,并且能够实现所有功能: https://github.com/googlesamples/leanback-showcase

这就是我得到的:

enter image description here

源代码如下:

所有逻辑均放在两个文件中

    import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import androidx.leanback.media.PlaybackGlue;
import androidx.leanback.media.PlaybackTransportControlGlue;

import java.io.File;

/**
 * Sample PlaybackSeekDataProvider that reads bitmaps stored on disk.
 * e.g. new PlaybackSeekDiskDataProvider(duration, 1000, "/sdcard/frame_%04d.jpg")
 * Expects the seek positions are 1000ms interval, snapshots are stored at
 * /sdcard/frame_0001.jpg, ...
 */
public class PlaybackSeekDiskDataProvider extends PlaybackSeekAsyncDataProvider {

    final Paint mPaint;
    final String mPathPattern;

    PlaybackSeekDiskDataProvider(long duration, long interval, String pathPattern) {
        mPathPattern = pathPattern;
        int size = (int) (duration / interval) + 1;
        long[] pos = new long[size];
        for (int i = 0; i < pos.length; i++) {
            pos[i] = i * duration / pos.length;
        }
        setSeekPositions(pos);
        mPaint = new Paint();
        mPaint.setTextSize(16);
        mPaint.setColor(Color.BLUE);
    }

    protected Bitmap doInBackground(Object task, int index, long position) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            // Thread might be interrupted by cancel() call.
        }
        if (isCancelled(task)) {
            return null;
        }
        String path = String.format(mPathPattern, (index + 1));
        if (new File(path).exists()) {
            return BitmapFactory.decodeFile(path);
        } else {
            Bitmap bmp = Bitmap.createBitmap(160, 160, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bmp);
            canvas.drawColor(Color.YELLOW);
            canvas.drawText(path, 10, 80, mPaint);
            canvas.drawText(Integer.toString(index), 10, 150, mPaint);
            return bmp;
        }
    }

    /**
     * Helper function to set a demo seek provider on PlaybackTransportControlGlue based on
     * duration.
     */
    public static void setDemoSeekProvider(final PlaybackTransportControlGlue glue) {
        if (glue.isPrepared()) {
            glue.setSeekProvider(new PlaybackSeekDiskDataProvider(
                    glue.getDuration(),
                    glue.getDuration() / 100,
                    "/sdcard/seek/frame_%04d.jpg"));
        } else {
            glue.addPlayerCallback(new PlaybackGlue.PlayerCallback() {
                @Override
                public void onPreparedStateChanged(PlaybackGlue glue) {
                    if (glue.isPrepared()) {
                        glue.removePlayerCallback(this);
                        PlaybackTransportControlGlue transportControlGlue =
                                (PlaybackTransportControlGlue) glue;
                        transportControlGlue.setSeekProvider(new PlaybackSeekDiskDataProvider(
                                transportControlGlue.getDuration(),
                                transportControlGlue.getDuration() / 100,
                                "/sdcard/seek/frame_%04d.jpg"));
                    }
                }
            });
        }
    }
}

在这一个中:

    import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.util.LruCache;
import android.util.SparseArray;

import androidx.leanback.widget.PlaybackSeekDataProvider;

import java.util.Iterator;
import java.util.Map;

public abstract class PlaybackSeekAsyncDataProvider extends PlaybackSeekDataProvider {

    static final String TAG = "SeekAsyncProvider";

    long[] mSeekPositions;
    // mCache is for the bitmap requested by user
    final LruCache<Integer, Bitmap> mCache;
    // mPrefetchCache is for the bitmap not requested by user but prefetched by heuristic
    // estimation. We use a different LruCache so that items in mCache will not be evicted by
    // prefeteched items.
    final LruCache<Integer, Bitmap> mPrefetchCache;
    final SparseArray<LoadBitmapTask> mRequests = new SparseArray<>();
    int mLastRequestedIndex = -1;

    protected boolean isCancelled(Object task) {
        return ((AsyncTask) task).isCancelled();
    }

    protected abstract Bitmap doInBackground(Object task, int index, long position);

    class LoadBitmapTask extends AsyncTask<Object, Object, Bitmap> {

        int mIndex;
        ResultCallback mResultCallback;

        LoadBitmapTask(int index, ResultCallback callback) {
            mIndex = index;
            mResultCallback = callback;
        }

        @Override
        protected Bitmap doInBackground(Object[] params) {
            return PlaybackSeekAsyncDataProvider.this
                    .doInBackground(this, mIndex, mSeekPositions[mIndex]);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            mRequests.remove(mIndex);
            Log.d(TAG, "thumb Loaded " + mIndex);
            if (mResultCallback != null) {
                mCache.put(mIndex, bitmap);
                mResultCallback.onThumbnailLoaded(bitmap, mIndex);
            } else {
                mPrefetchCache.put(mIndex, bitmap);
            }
        }

    }

    public PlaybackSeekAsyncDataProvider() {
        this(16, 24);
    }

    public PlaybackSeekAsyncDataProvider(int cacheSize, int prefetchCacheSize) {
        mCache = new LruCache<Integer, Bitmap>(cacheSize);
        mPrefetchCache = new LruCache<Integer, Bitmap>(prefetchCacheSize);
    }

    public void setSeekPositions(long[] positions) {
        mSeekPositions = positions;
    }

    @Override
    public long[] getSeekPositions() {
        return mSeekPositions;
    }

    @Override
    public void getThumbnail(int index, ResultCallback callback) {
        Integer key = index;
        Bitmap bitmap = mCache.get(key);
        if (bitmap != null) {
            callback.onThumbnailLoaded(bitmap, index);
        } else {
            bitmap = mPrefetchCache.get(key);
            if (bitmap != null) {
                mCache.put(key, bitmap);
                mPrefetchCache.remove(key);
                callback.onThumbnailLoaded(bitmap, index);
            } else {
                LoadBitmapTask task = mRequests.get(index);
                if (task == null || task.isCancelled()) {
                    // no normal task or prefetch for the position, create a new task
                    task = new LoadBitmapTask(index, callback);
                    mRequests.put(index, task);
                    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                } else {
                    // update existing ResultCallback which might be normal task or prefetch
                    task.mResultCallback = callback;
                }
            }
        }
        if (mLastRequestedIndex != index) {
            if (mLastRequestedIndex != -1) {
                prefetch(mLastRequestedIndex, index > mLastRequestedIndex);
            }
            mLastRequestedIndex = index;
        }
    }

    protected void prefetch(int hintIndex, boolean forward) {
        for (Iterator<Map.Entry<Integer, Bitmap>> it =
             mPrefetchCache.snapshot().entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<Integer, Bitmap> entry = it.next();
            if (forward ? entry.getKey() < hintIndex : entry.getKey() > hintIndex) {
                mPrefetchCache.remove(entry.getKey());
            }
        }
        int inc = forward ? 1 : -1;
        for (int i = hintIndex; (mRequests.size() + mPrefetchCache.size()
                < mPrefetchCache.maxSize()) && (inc > 0 ? i < mSeekPositions.length : i >= 0);
             i += inc) {
            Integer key = i;
            if (mCache.get(key) == null && mPrefetchCache.get(key) == null) {
                LoadBitmapTask task = mRequests.get(i);
                if (task == null) {
                    task = new LoadBitmapTask(key, null);
                    mRequests.put(i, task);
                    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                }
            }
        }
    }

    @Override
    public void reset() {
        for (int i = 0; i < mRequests.size(); i++) {
            LoadBitmapTask task = mRequests.valueAt(i);
            task.cancel(true);
        }
        mRequests.clear();
        mCache.evictAll();
        mPrefetchCache.evictAll();
        mLastRequestedIndex = -1;
    }

    @Override
    public String toString() {
        StringBuilder b = new StringBuilder();
        b.append("Requests<");
        for (int i = 0; i < mRequests.size(); i++) {
            b.append(mRequests.keyAt(i));
            b.append(",");
        }
        b.append("> Cache<");
        for (Iterator<Integer> it = mCache.snapshot().keySet().iterator(); it.hasNext();) {
            Integer key = it.next();
            if (mCache.get(key) != null) {
                b.append(key);
                b.append(",");
            }
        }
        b.append(">");
        b.append("> PrefetchCache<");
        for (Iterator<Integer> it = mPrefetchCache.snapshot().keySet().iterator(); it.hasNext();) {
            Integer key = it.next();
            if (mPrefetchCache.get(key) != null) {
                b.append(key);
                b.append(",");
            }
        }
        b.append(">");
        return b.toString();
    }
}

我非常感谢您的帮助。谢谢!

0 个答案:

没有答案