VideoFloatHelper.kt 5.53 KB
Newer Older
严久程 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package com.yidianling.course.widget

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.ImageView
import com.alibaba.android.arouter.launcher.ARouter
import com.dou361.ijkplayer.widget.PlayStateParams
import com.dou361.ijkplayer.widget.PlayerView
import com.lzf.easyfloat.EasyFloat
import com.lzf.easyfloat.enums.ShowPattern
import com.lzf.easyfloat.interfaces.OnInvokeView
严久程 committed
15
import com.ydl.media.audio.utils.PlayProgressUtil
严久程 committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
import com.yidianling.common.tools.RxDeviceTool
import com.yidianling.common.tools.RxImageTool
import com.yidianling.course.R
import com.yidianling.course.coursePlay.CoursePlayActivity
import com.yidianling.course.flutterPlugin.CourseSendPlugin
import com.yidianling.course.uitls.VideoProgressUtil
import java.util.*

/**
 * @author jiucheng
 * @描述:视频悬浮窗
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/11/19
 */
object VideoFloatHelper {
    private const val courseTag = "course_video_play"
    var courseVideoUrl = ""
    var courseId = ""
    var isCanClick = true
    var isCurrentVideoPlaying = true
    //视频播放器view
    @SuppressLint("StaticFieldLeak")
    private var videoView: PlayerView? = null
    private var timer: Timer? = null
    var defaultShowPattern = ShowPattern.CURRENT_ACTIVITY

    fun setVideoInfo(
        courseId: String,
        courseVideoUrl: String,
        isCanClick: Boolean
    ): VideoFloatHelper {
        this.courseId = courseId
        this.courseVideoUrl = courseVideoUrl
        this.isCanClick = isCanClick
        return this
    }


    fun showVideoFloat(activity: Activity) {
        val x = RxDeviceTool.getScreenWidth(activity) - RxImageTool.dp2px(220f)
        val y = RxDeviceTool.getScreenHeight(activity) * 3 / 4

        EasyFloat.with(activity)
            .setTag(courseTag)
            .setShowPattern(defaultShowPattern)
            .setLocation(x, y)
            .setAppFloatAnimator(null)
            .setFilter(CoursePlayActivity::class.java)
            .setLayout(R.layout.course_float_video_view, OnInvokeView {
                it.findViewById<ImageView>(R.id.iv_video_close).setOnClickListener {
                    dismissFloat(activity)
                }
                val videoFullScreen = it.findViewById<ImageView>(R.id.iv_video_full_screen)
                if (isCanClick) {
                    videoFullScreen.visibility = View.VISIBLE
                } else {
                    videoFullScreen.visibility = View.INVISIBLE
                }
                videoFullScreen.setOnClickListener {
                    if (isCanClick) {
                        startCoursePlayActivity(activity, 1, 1, courseVideoUrl, true)
                    }
                }

                val videoLayout = it.findViewById<ImageView>(R.id.app_video_box)

                try {
                    initVideoPlayer(activity, videoLayout)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            })
            .show()
    }

    private fun initVideoPlayer(activity: Activity, view: View) {
        val url = courseVideoUrl.replace("https", "http")
严久程 committed
94
        val hisTime = PlayProgressUtil.getProgress(activity, url)
严久程 committed
95 96 97 98 99 100 101 102 103
        videoView = PlayerView(activity, view)
            .setScaleType(PlayStateParams.fitparent)
            .hideAllUI()
            .setNetWorkTypeTie(false)
            .setAutoReConnect(true, 3)
            .forbidTouch(true)
            .setOnInfoListener { _, what, _ ->
                if (what == PlayStateParams.STATE_COMPLETED) {
                    isCurrentVideoPlaying = false
严久程 committed
104
                    PlayProgressUtil.saveProgress(activity, url, 0)
严久程 committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
                } else {
                    isCurrentVideoPlaying = true
                }

                true
            }
            .setPlaySource(url)
            .startPlay()
            .seekTo(hisTime)

        view.setOnClickListener {
            if (isCanClick) {
                startCoursePlayActivity(activity, 1, 0, courseVideoUrl, true)
            }
        }

        startTimer(url, activity)
    }


    private fun startTimer(url: String, activity: Activity) {
        if (timer == null) {
            timer = Timer()
        }
        timer?.schedule(object : TimerTask() {
            override fun run() {
                if (isCurrentVideoPlaying) {
                    var time = videoView?.currentPosition ?: 0
                    if (time < 3000) return
严久程 committed
134
                    PlayProgressUtil.saveProgress(activity, url, time)
严久程 committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
                }
            }
        }, 1000, 1000)
    }


    private fun startCoursePlayActivity(
        context: Context?,
        from: Int,
        fullScreen: Int,
        coursePlayUrl: String,
        isFromFloatView: Boolean
    ) {
        ARouter.getInstance()
            .build("/course/play")
            .withInt("course_id", courseId.toInt())
            .withInt("course_type", 1)
            .withString("coursePlayUrl", coursePlayUrl)
            .withInt("from", from)
            .withBoolean("isFromFloatView", isFromFloatView)
            .withInt("fullScreen", fullScreen)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            .navigation()
    }


    fun dismissFloat(activity: Activity) {
        CourseSendPlugin.sendMsg(false)
YKai committed
163
        EasyFloat.dismissAppFloat( courseTag)
严久程 committed
164 165 166 167 168 169 170 171 172 173
        if (videoView != null) {
            videoView!!.stopPlay()
            videoView = null
        }
        if (timer != null) {
            timer!!.cancel()
        }
        timer = null
    }
}