CoursePlayItemViewVideo.kt 11.2 KB
Newer Older
严久程 committed
1 2 3 4 5 6 7 8 9 10
package com.yidianling.course.coursePlay

import android.annotation.SuppressLint
import android.content.Context
import android.os.Handler
import android.os.PowerManager
import android.view.View
import android.widget.RelativeLayout
import com.dou361.ijkplayer.widget.PlayStateParams
import com.dou361.ijkplayer.widget.PlayerView
严久程 committed
11 12 13
import com.ydl.media.audio.utils.PlayProgressUtil
import com.ydl.media.view.PlayTypeEnum
import com.ydl.media.view.PlayerFloatHelper
严久程 committed
14
import com.ydl.ydl_image.module.GlideApp
严久程 committed
15
import com.ydl.ydlcommon.utils.LogUtil
严久程 committed
16
import com.ydl.ydlcommon.view.dialog.CommonDialog
严久程 committed
17
import com.yidianling.course.R
严久程 committed
18 19
import com.yidianling.course.bean.CourseExtraBean
import com.yidianling.course.bean.CourseMediaBean
严久程 committed
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
import tv.danmaku.ijk.media.player.IMediaPlayer
import java.text.SimpleDateFormat
import java.util.*


/**
 * 课程播放器的item
 * Created by harvie on 2017/6/26 0026.
 */
class CoursePlayItemViewVideo : RelativeLayout, PlayViewInterface {
    //播放列表
    private var playList: ArrayList<CourseMediaBean> = ArrayList()
    private var courseExtra: CourseExtraBean? = null
    var activity: CoursePlayActivity? = null

    var videoUrl: String? = null

    //播放模式
    var playMoudle = 0 //默认为0 音频播放  1视频播放

    var timer = Timer()

    var index = 0
    private var isVideoPlaying = true
    private var mHandler = Handler()
    var hasEnsureNetStatus = false

    private val formatter = SimpleDateFormat("mm:ss", Locale.getDefault())
    private var wakeLock: PowerManager.WakeLock? = null

    companion object {
        fun create(activity: CoursePlayActivity, playType: Int): CoursePlayItemViewVideo {
            return CoursePlayItemViewVideo(activity, playType)
        }
    }

    @SuppressLint("InvalidWakeLockTag")
    private constructor(context: CoursePlayActivity, playMoudle: Int) : super(context) {
        this.playMoudle = playMoudle
严久程 committed
59
        View.inflate(context, R.layout.course_view_course_play_item, this)
严久程 committed
60 61 62 63
        activity = context
        initView()

        wakeLock = (activity!!.getSystemService(Context.POWER_SERVICE) as PowerManager)
64 65 66 67
            .newWakeLock(
                PowerManager.SCREEN_BRIGHT_WAKE_LOCK or PowerManager.ON_AFTER_RELEASE,
                "CoursePlayItemViewVideo"
            )
严久程 committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    }

    override fun onNewIntent() {
    }

    fun initView() {
        findViewById<View>(R.id.simple_player_settings_container).visibility = View.GONE
        findViewById<View>(R.id.app_video_center_box).visibility = View.GONE //
        setListener()
        //开启定时器实时记录播放位置
        startTimer()
    }

    private fun startTimer() {
        timer?.schedule(object : TimerTask() {
            override fun run() {
                if (isVideoPlaying) {
                    var time = activity?.videoView?.currentPosition ?: 0
                    if (time < 3000) return
严久程 committed
87
                    PlayProgressUtil.saveProgress(context, videoUrl, time)
严久程 committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
                }
            }
        }, 1000, 1000)
    }


    //设置监听事件
    private fun setListener() {
    }


    private fun playNext() {
//        var size: Int = playList.size
//        if ((size - 1) > bean?.attachmentIndex ?: 0) {
//            //这里请勿给index先赋值,判断可播放后会自动赋值
//            val index = bean?.attachmentIndex ?: 0
//            play(index!! + 1, true)
//            UtilH.saveProgress(activity, playList[index].playUrl, 0);
//        }
    }

    override fun onResume() {
        if (wakeLock != null) {
            wakeLock!!.acquire()
        }
        activity?.videoView?.onResume()
严久程 committed
114
        var hisTime1 = PlayProgressUtil.getProgress(context, videoUrl)
严久程 committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        activity?.videoView?.seekTo(hisTime1)
    }


    override fun onPause() {
        if (wakeLock != null) {
            if (wakeLock!!.isHeld) {
                wakeLock!!.release()
            }
        }
    }

    /**
     * 播放音频
     * index 播放下标
     */
    fun play(index: Int) {
        this.index = index
        var canPlay = false
        //判断当前课程是否试听
        if (playList[index].isDemo) {
            canPlay = true
        } else {
            //非试听
            if (courseExtra!!.isBuy) {
                //已购买
                canPlay = true
            } else {
                //未购买,判断是否是试听课程
                //即未购买,也不是试听,弹窗提示
                CommonDialog(activity)
146 147 148 149 150 151 152 153 154 155
                    .setMessage("\n购买课程,获取完整课程内容\n")
                    .setLeftOnclick("放弃") {

                    }
                    .setRightClick("购买") {
                        //跳转支付页
                        activity?.addCourseOrder()
                    }
                    .setCancelAble(false)
                    .show()
严久程 committed
156 157 158
            }
        }
        if (canPlay) {
159
            PlayerFloatHelper.playingType = PlayTypeEnum.PLAY_TYPE_COURSE
严久程 committed
160

严久程 committed
161 162

            var url = playList[index].url
163
//            url = url.replace("https", "http")
严久程 committed
164 165 166

            videoUrl = url

严久程 committed
167
            var hisTime = PlayProgressUtil.getProgress(context, url)
严久程 committed
168
            try {
169

严久程 committed
170
                activity?.videoView = PlayerView(activity)
171 172 173 174 175 176 177 178 179 180 181
                    .setScaleType(PlayStateParams.fitparent)
                    .hideMenu(true)
                    .hideRotation(true)
                    .setNetWorkTypeTie(!hasEnsureNetStatus)
                    .hideBack(true)
                    .hideSteam(true)
                    .setAutoReConnect(true, 3)
                    .forbidTouch(false)
                    .setOnInfoListener(object : IMediaPlayer.OnInfoListener {
                        override fun onInfo(mp: IMediaPlayer, what: Int, extra: Int): Boolean {
                            LogUtil.e("课程播放--状态-$what")
182 183 184 185
                            if (what == PlayStateParams.MEDIA_INFO_VIDEO_INTERRUPT) {
                                activity?.videoView?.startPlay()
                            }

186 187 188 189 190 191 192
                            if (what == PlayStateParams.STATE_PAUSED) {
                                this@CoursePlayItemViewVideo.findViewById<View>(R.id.rl_play_pause_layout)
                                    .visibility = View.VISIBLE
                            } else {
                                this@CoursePlayItemViewVideo.findViewById<View>(R.id.rl_play_pause_layout)
                                    .visibility = View.GONE
                            }
严久程 committed
193

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
                            if (what == PlayStateParams.STATE_PLAYING) {
                                this@CoursePlayItemViewVideo.findViewById<View>(R.id.app_video_loading)
                                    .visibility = View.GONE
                            }
                            if (what == PlayStateParams.MEDIA_INFO_BUFFERING_END
                                || what == PlayStateParams.MEDIA_INFO_AUDIO_RENDERING_START
                                || what == PlayStateParams.MEDIA_INFO_VIDEO_RENDERING_START
                                || what == PlayStateParams.STATE_PLAYING
                                || what == IMediaPlayer.MEDIA_INFO_VIDEO_SEEK_RENDERING_START
                            ) {
                                activity?.isVideoPlay = true
                                this@CoursePlayItemViewVideo.findViewById<View>(R.id.ll_bg)
                                    .visibility = View.GONE
                                this@CoursePlayItemViewVideo.findViewById<View>(R.id.ll_loading)
                                    .visibility = View.GONE
                                this@CoursePlayItemViewVideo.findViewById<View>(R.id.app_video_loading)
                                    .visibility = View.GONE
                            }
严久程 committed
212

213 214 215 216 217 218 219 220 221 222 223 224 225
                            if (what == PlayStateParams.STATE_COMPLETED) {
                                isVideoPlaying = false
                                PlayProgressUtil.saveProgress(
                                    this@CoursePlayItemViewVideo.activity,
                                    videoUrl,
                                    0
                                )
                                if (playList.size - 1 > this@CoursePlayItemViewVideo.index) {
                                    //播放下一曲
//                                        play(this@CoursePlayItemViewVideo.index + 1)
                                    this@CoursePlayItemViewVideo.index =
                                        this@CoursePlayItemViewVideo.index + 1
                                    activity!!.updatePlayingListStatus(this@CoursePlayItemViewVideo.index)
226 227 228 229
                                    this@CoursePlayItemViewVideo.findViewById<View>(R.id.ll_loading)
                                        .visibility = View.VISIBLE
                                    this@CoursePlayItemViewVideo.findViewById<View>(R.id.app_video_loading)
                                        .visibility = View.VISIBLE
严久程 committed
230
                                }
231 232
                            } else {
                                isVideoPlaying = true
严久程 committed
233
                            }
234 235 236 237 238 239 240 241 242
                            return true
                        }
                    })
                    .showThumbnail { ivThumbnail ->
                        run {
                            ivThumbnail.alpha = 0.5f
                            GlideApp.with(context)
                                .load(courseExtra!!.pic)
                                .into(ivThumbnail)
严久程 committed
243
                        }
244 245 246 247
                    }
                    .setPlaySource(url)
                    .startPlay()
                    .seekTo(hisTime)
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
//                try {
//                    var videoViewField =
//                        activity?.videoView.run { javaClass.getDeclaredField("videoView") }
//                    videoViewField.isAccessible = true
//
//                    var ijkVideoView = videoViewField.get(activity?.videoView) as IjkVideoView
//                    var mMediaPlayerField = ijkVideoView.javaClass.getDeclaredField("mMediaPlayer")
//
//                    var mMediaPlayerObject = mMediaPlayerField.get(ijkVideoView)
//                    if (mMediaPlayerObject is IjkMediaPlayer) {
//                        mMediaPlayerObject.setOption(
//                            IjkMediaPlayer.OPT_CATEGORY_FORMAT,
//                            "dns_cache_clear",
//                            1
//                        )
//                    }
//                } catch (e: java.lang.Exception) {
//
//                }
//
//                activity?.videoView?.startPlay()?.seekTo(hisTime)

严久程 committed
270 271 272 273 274 275 276 277 278 279 280 281 282
                val layout = activity?.window?.attributes
                layout?.screenBrightness = -1f
                activity?.window?.attributes = layout

            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    /**
     * 设置显示数据
     */
283 284 285 286
    override fun setData(
        index: Int,
        list: ArrayList<CourseMediaBean>,
        courseExtra: CourseExtraBean,
287 288
        from: Int,
        isAutoPlay: Boolean
289
    ) {
严久程 committed
290 291 292 293 294

        if (list.isEmpty()) return
        playList.clear()
        playList.addAll(list)
        this.courseExtra = courseExtra
295 296 297
        if (isAutoPlay) {
            play(index)
        }
严久程 committed
298 299 300 301 302 303
    }

    override fun onDestroy() {
        timer.cancel()
    }
}