在自定义FrameLayout实现中直接从URL播放视频

时间:2019-07-15 12:37:43

标签: android cloudinary android-video-player

我有以下从Framelayout继承的类-


public class FirebasePlayerView extends FrameLayout {
    private ImageView mCoverImageView;
    private Video mVideo;
    private MDPlayerView mPlayerView;
    private ExtrasViewCreator mExtrasViewCreator;
    private MediaSource mMediaSource;
    private ExoPlayer mExoPlayer;
    private boolean mPreventAutoTogglePlaying;

    public FirebasePlayerView(@NonNull Context context) {
        super(context);
        init();
    }

    public FirebasePlayerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FirebasePlayerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public FirebasePlayerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        // default
        mPreventAutoTogglePlaying = false;

        // layout
        inflate(getContext(), R.layout.custom_firebase_player_view, this);

        // init view
        mPlayerView = findViewById(R.id.custom_firebase_md_player_view);
        setExtrasViewCreator(createDefaultExtrasViewCreator());

        // Cover Image
        mCoverImageView = new ImageView(getContext());
        mCoverImageView.setBackgroundColor(Color.BLACK);
        mPlayerView.setCoverView(mCoverImageView);
    }

    private ExtrasViewCreator createDefaultExtrasViewCreator() {
        return new DefaultExtrasViewCreator(getContext());
    }

    private void setPlayerExtrasView(View extrasView) {
        mPlayerView.setCustomExtraView(extrasView);
    }

    public @NonNull
    MDPlayerView getPlayerView() {
        return mPlayerView;
    }

    public void setVideo(Video video) {
        if (mVideo != video) {
            mVideo = video;
            reloadData();
        }
    }

    public ExoPlayer getExoPlayer() {
        return mExoPlayer;
    }

    public void setPlayer(ExoPlayer exoPlayer) {
        mPlayerView.setPlayer(exoPlayer);
        mExoPlayer = exoPlayer;

        bindPlayerIfReady();
    }

    public ExtrasViewCreator getExtrasViewCreator() {
        return mExtrasViewCreator;
    }

    public void setExtrasViewCreator(ExtrasViewCreator extrasViewCreator) {
        mExtrasViewCreator = extrasViewCreator;

        // extrasView
        if (mExtrasViewCreator != null) {
            View extrasView = mExtrasViewCreator.getView();
            setPlayerExtrasView(extrasView);

            // bind video if ready
            if (mVideo != null) {
                mExtrasViewCreator.bindExtrasView(mVideo);
            }
        } else {
            setPlayerExtrasView(null);
        }
    }

    private void reloadData() {
        if (mVideo != null) {
            // thumb
            ImageUtil.loadFirebaseImageUri(mVideo.getThumbUri(), mCoverImageView);

            // default extras
            if (mExtrasViewCreator != null) {
                mExtrasViewCreator.bindExtrasView(mVideo);
            }

            // auto load download uri
            loadVideoUri(mVideo.getStorageUri());
        } else {
            // clear
            mCoverImageView.setImageResource(0);
        }
    }

    private void loadVideoUri(String storageUri) {
        if (StringUtils.isBlank(storageUri)) {
            return;
        }
        //firebase URI
        // load firebase storage
        if (!storageUri.contains("cloudinary")) {
            //firebase URL
            Task<Uri> downloadUrlTask = FirebaseStorage.getInstance().getReferenceFromUrl(storageUri).getDownloadUrl();
            if (getContext() instanceof Activity) {
                downloadUrlTask.addOnCompleteListener((Activity) getContext(), mOnDownloadUrlCompleted);
            } else {
                downloadUrlTask.addOnCompleteListener(mOnDownloadUrlCompleted);
            }
        } else {
            // cloudinary URI

        }


    }

    private OnCompleteListener<Uri> mOnDownloadUrlCompleted = task -> {
        if (task.isSuccessful()) {
            // cache value
            Uri videoDownloadUri = task.getResult();
            Timber.d("video uri loaded: %s", videoDownloadUri);

            // build MediaSource
            onDownloadUriReady(videoDownloadUri);
        } else {
            // show error
            Timber.e(task.getException(), "Can not get video download url");
        }
    };

    private void onDownloadUriReady(Uri videoDownloadUri) {
        if (mVideo != null && StringUtils.isNotBlank(mVideo.getStorageUri()) && videoDownloadUri != null) {
            // cache if need
            if (mShouldCacheVideo) {
                Timber.d("Request cache video: %s -> %s", mVideo.getStorageUri(), videoDownloadUri);
                ExoCacheProvider.getInstance().enqueueVideo(mVideo.getStorageUri(), videoDownloadUri);
            }

            // create media source
            onMediaSourceLoaded(ExoCacheProvider.getInstance().createMediaSource(mVideo.getStorageUri(), videoDownloadUri));
        }
    }

    private void onMediaSourceLoaded(MediaSource mediaSource) {
        // create media source
        mMediaSource = mediaSource;

        // attach to player
        bindPlayerIfReady();
    }

    private void bindPlayerIfReady() {
        if (readyToPrepareVideo()) {
            // simply play video
            mExoPlayer.prepare(mMediaSource);
        }
    }

    public boolean isPreventAutoTogglePlaying() {
        return mPreventAutoTogglePlaying;
    }

    public void setPreventAutoTogglePlaying(boolean preventAutoTogglePlaying) {
        mPreventAutoTogglePlaying = preventAutoTogglePlaying;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // prevent passing touch event to child
        return isPreventAutoTogglePlaying() || super.onInterceptTouchEvent(ev);
    }

    private boolean readyToPrepareVideo() {
        return mMediaSource != null && mExoPlayer != null;
    }

    private boolean mShouldCacheVideo;

    public void enqueueCacheVideo() {
        mShouldCacheVideo = true;
    }
}


此类仅用于Firebase加载功能,但我添加了Cloudinary功能,在以下部分中,我还为视频或图像提供了有效的HTTP URL-


private void loadVideoUri(String storageUri) {
        if (StringUtils.isBlank(storageUri)) {
            return;
        }
        //firebase URI
        // load firebase storage
        if (!storageUri.contains("cloudinary")) {
            //firebase URL
            Task<Uri> downloadUrlTask = FirebaseStorage.getInstance().getReferenceFromUrl(storageUri).getDownloadUrl();
            if (getContext() instanceof Activity) {
                downloadUrlTask.addOnCompleteListener((Activity) getContext(), mOnDownloadUrlCompleted);
            } else {
                downloadUrlTask.addOnCompleteListener(mOnDownloadUrlCompleted);
            }
        } else {
            // cloudinary URI
            // ->> here is my implementation issue
        }

    }


,我正努力按照以下方法实施视频。有人可以指出我所缺少的吗?如果我错过了什么?

0 个答案:

没有答案