CoursePlayItemViewAudio.kt 6.33 KB
Newer Older
严久程 committed
1 2 3 4 5 6
package com.yidianling.course.coursePlay

import android.annotation.SuppressLint
import android.text.TextUtils
import android.view.ViewGroup
import android.widget.RelativeLayout
7 8 9
import com.ydl.media.audio.AudioPlayer
import com.ydl.media.audio.model.Music
import com.ydl.ydlcommon.view.dialog.CommonDialog
严久程 committed
10 11
import com.yidianling.common.tools.RxDeviceTool
import com.yidianling.common.tools.RxNetTool
严久程 committed
12 13
import com.yidianling.course.bean.CourseExtraBean
import com.yidianling.course.bean.CourseMediaBean
14
import com.yidianling.course.listener.HPlayStatusListener
严久程 committed
15
import com.yidianling.course.widget.HPlayView
严久程 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


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

    var hasEnsureNetStatus = false
    var currentIndex = 0

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

    @SuppressLint("InvalidWakeLockTag")
    private constructor(context: CoursePlayActivity) : super(context) {
        playView = HPlayView(context)
        var width = RxDeviceTool.getScreenWidth(context)
        var heigth = width * 9 / 16
        var layoutP = ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, heigth)
        playView?.layoutParams = layoutP
        this.addView(playView)
        activity = context
        initView()

    }

    fun initView() {
        setListener()
    }

    override fun onResume() {

    }

    override fun onPause() {

    }

    override fun onNewIntent() {
    }

    //设置监听事件
    fun setListener() {
        playView?.listener = object : HPlayStatusListener {
69
            override fun isCanPlay(data: Music?): Boolean {
严久程 committed
70
                var canPlay = false
严久程 committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
                //非试听
                if (courseExtra!!.isBuy) {
                    //已购买
                    canPlay = true
                } else {
                    //未购买,判断是否是试听课程
                    //即未购买,也不是试听,弹窗提示
                    if (activity == null) return false
                    CommonDialog(activity)
                        .setMessage("\n购买课程,获取完整课程内容\n")
                        .setLeftOnclick("放弃") {

                        }
                        .setRightClick("购买") {
                            //跳转支付页
                            activity?.addCourseOrder()
                        }
                        .setCancelAble(false)
                        .show()
                }
严久程 committed
91 92 93
                return canPlay
            }

94
            override fun onPrepared(data: Music?, index: Int) {
严久程 committed
95 96 97 98 99 100 101 102 103 104
                //更新播放列表的状态
                activity!!.updatePlayingListStatus(index)
            }

        }
    }

    /**
     * 设置显示数据
     */
严久程 committed
105 106 107 108
    override fun setData(
        index: Int,
        list: ArrayList<CourseMediaBean>,
        courseExtra: CourseExtraBean,
109 110
        from: Int,
        isAutoPlay: Boolean
严久程 committed
111
    ) {
严久程 committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

        if (list.isEmpty()) return
        playList.clear()
        playList.addAll(list)
        this.courseExtra = courseExtra
        currentIndex = index

        if (courseExtra.isBuy) {
            playView?.setAutoNext(true)
        } else {
            //未购买,取消自动播放
            playView?.setAutoNext(false)
        }
        playView?.setImageBackground(courseExtra.pic)

        playView!!.setData(stringToMusicPlayerList(playList))


130
        if (AudioPlayer.get().isPlaying) {
严久程 committed
131 132 133 134
            hasEnsureNetStatus = true
        }

        if (RxNetTool.isWifi(activity!!)) {
135
            if (AudioPlayer.get().isPlaying && (from == 1 || from == 2)) {
严久程 committed
136 137 138
                if (TextUtils.equals(playView?.getCurrentUrl(), list[currentIndex].url)) {
                    playView?.updateView(currentIndex)
                } else {
139 140 141
                    if (isAutoPlay){
                        playView?.play(currentIndex)
                    }
严久程 committed
142 143
                }
            } else {
144 145 146
                if (isAutoPlay){
                    playView?.play(currentIndex)
                }
严久程 committed
147 148 149
            }
        } else {
            if (hasEnsureNetStatus) {
150
                if (AudioPlayer.get().isPlaying && (from == 1 || from == 2)) {
严久程 committed
151 152 153
                    if (TextUtils.equals(playView?.getCurrentUrl(), list[currentIndex].url)) {
                        playView?.updateView(currentIndex)
                    } else {
154 155 156
                        if (isAutoPlay){
                            playView?.play(currentIndex)
                        }
严久程 committed
157 158
                    }
                } else {
159 160 161
                    if (isAutoPlay){
                        playView?.play(currentIndex)
                    }
严久程 committed
162 163 164 165
                }
            } else {
                playView?.showNetNotice {
                    hasEnsureNetStatus = true
166
                    if (AudioPlayer.get().isPlaying && (from == 1 || from == 2)) {
严久程 committed
167 168 169
                        if (TextUtils.equals(playView?.getCurrentUrl(), list[currentIndex].url)) {
                            playView?.updateView(currentIndex)
                        } else {
170 171 172
                            if (isAutoPlay){
                                playView?.play(currentIndex)
                            }
严久程 committed
173 174
                        }
                    } else {
175 176 177
                        if (isAutoPlay){
                            playView?.play(currentIndex)
                        }
严久程 committed
178 179 180 181 182 183 184
                    }
                }
            }
        }
    }

    //将列表转为播放器可用列表
185 186
    private fun stringToMusicPlayerList(list: List<CourseMediaBean>): ArrayList<Music> {
        var nlist = ArrayList<Music>()
严久程 committed
187 188
        try {
            for (i in list.indices) {
189 190 191
                var be = Music()
                be.path = list[i].url
                be.coverPath = courseExtra!!.pic
严久程 committed
192
                be.title = list[i].title
193
                be.artist = list[i].doctorName
严久程 committed
194 195 196 197 198 199 200 201 202
                nlist.add(be)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return nlist
    }

    override fun onDestroy() {
严久程 committed
203 204
        playView?.onDestroy()
        playView = null
严久程 committed
205 206
    }
}