CoursePlayItemViewVideo.kt 10.5 KB
Newer Older
严久程 committed
1 2 3 4 5 6 7 8
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
9
import com.dou361.ijkplayer.widget.IjkVideoView
严久程 committed
10 11
import com.dou361.ijkplayer.widget.PlayStateParams
import com.dou361.ijkplayer.widget.PlayerView
严久程 committed
12 13 14
import com.ydl.media.audio.utils.PlayProgressUtil
import com.ydl.media.view.PlayTypeEnum
import com.ydl.media.view.PlayerFloatHelper
严久程 committed
15
import com.ydl.ydl_image.module.GlideApp
严久程 committed
16
import com.ydl.ydlcommon.utils.LogUtil
严久程 committed
17
import com.ydl.ydlcommon.view.dialog.CommonDialog
严久程 committed
18
import com.yidianling.course.R
严久程 committed
19 20
import com.yidianling.course.bean.CourseExtraBean
import com.yidianling.course.bean.CourseMediaBean
21
import tv.danmaku.ijk.media.player.AndroidMediaPlayer
严久程 committed
22
import tv.danmaku.ijk.media.player.IMediaPlayer
23
import tv.danmaku.ijk.media.player.IjkMediaPlayer
严久程 committed
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
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
61
        View.inflate(context, R.layout.course_view_course_play_item, this)
严久程 committed
62 63 64 65
        activity = context
        initView()

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

    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
89
                    PlayProgressUtil.saveProgress(context, videoUrl, time)
严久程 committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
                }
            }
        }, 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
116
        var hisTime1 = PlayProgressUtil.getProgress(context, videoUrl)
严久程 committed
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 146 147
        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)
148 149 150 151 152 153 154 155 156 157
                    .setMessage("\n购买课程,获取完整课程内容\n")
                    .setLeftOnclick("放弃") {

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

严久程 committed
163 164

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

            videoUrl = url
严久程 committed
168
            var hisTime = PlayProgressUtil.getProgress(context, url)
169 170


严久程 committed
171
            try {
172

173
               /* activity?.videoView = PlayerView(activity)
174 175 176 177 178 179 180 181 182 183
                    .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 {
184

185
                            LogUtil.e("课程播放--状态-$what")
186 187 188 189
                            if (what == PlayStateParams.MEDIA_INFO_VIDEO_INTERRUPT) {
                                activity?.videoView?.startPlay()
                            }

190 191 192 193 194 195 196
                            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
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                            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
216

217 218 219 220 221 222 223 224 225 226 227 228 229
                            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)
230 231 232 233
                                    this@CoursePlayItemViewVideo.findViewById<View>(R.id.ll_loading)
                                        .visibility = View.VISIBLE
                                    this@CoursePlayItemViewVideo.findViewById<View>(R.id.app_video_loading)
                                        .visibility = View.VISIBLE
严久程 committed
234
                                }
235 236
                            } else {
                                isVideoPlaying = true
严久程 committed
237
                            }
238 239 240 241 242 243 244 245 246
                            return true
                        }
                    })
                    .showThumbnail { ivThumbnail ->
                        run {
                            ivThumbnail.alpha = 0.5f
                            GlideApp.with(context)
                                .load(courseExtra!!.pic)
                                .into(ivThumbnail)
严久程 committed
247
                        }
248
                    }
249

250 251
                    .setPlaySource(url)
                    .seekTo(hisTime)
252 253
                activity?.videoView?.setPlaySource(url)
                activity?.videoView?.startPlay()
严久程 committed
254 255
                val layout = activity?.window?.attributes
                layout?.screenBrightness = -1f
256
                activity?.window?.attributes = layout*/
严久程 committed
257 258 259

            } catch (e: Exception) {
                e.printStackTrace()
260
                LogUtil.e("aaaaaaaaa:"+e.message)
严久程 committed
261 262 263 264 265 266 267
            }
        }
    }

    /**
     * 设置显示数据
     */
268 269 270 271
    override fun setData(
        index: Int,
        list: ArrayList<CourseMediaBean>,
        courseExtra: CourseExtraBean,
272 273
        from: Int,
        isAutoPlay: Boolean
274
    ) {
严久程 committed
275 276 277 278 279

        if (list.isEmpty()) return
        playList.clear()
        playList.addAll(list)
        this.courseExtra = courseExtra
280 281 282
        if (isAutoPlay) {
            play(index)
        }
严久程 committed
283 284 285 286 287 288
    }

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