CoursePlayItemViewAudio.kt 6.24 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
                var canPlay = false
                    //非试听
                    if (courseExtra!!.isBuy) {
                        //已购买
                        canPlay = true
                    } else {
                        //未购买,判断是否是试听课程
                        //即未购买,也不是试听,弹窗提示
                        if (activity == null) return false
                        CommonDialog(activity)
                                .setMessage("\n购买课程,获取完整课程内容\n")
                                .setLeftOnclick("放弃") {

                                }
                                .setRightClick("购买") {
                                    //跳转支付页
                                    activity?.addCourseOrder()
                                }
                                .setCancelAble(false)
                                .show()
                    }
                return canPlay
            }

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

        }
    }

    /**
     * 设置显示数据
     */
    override fun setData(index: Int, list: ArrayList<CourseMediaBean>, courseExtra: CourseExtraBean, from: Int) {

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

113
//        YDLMusicHelper.course_id = courseExtra.id.toInt()
严久程 committed
114 115 116 117 118 119 120 121 122 123 124 125

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

        playView!!.setData(stringToMusicPlayerList(playList))


126
        if (AudioPlayer.get().isPlaying) {
严久程 committed
127 128 129 130
            hasEnsureNetStatus = true
        }

        if (RxNetTool.isWifi(activity!!)) {
131
            if (AudioPlayer.get().isPlaying && (from == 1 || from == 2)) {
严久程 committed
132 133 134 135 136 137 138 139 140 141
                if (TextUtils.equals(playView?.getCurrentUrl(), list[currentIndex].url)) {
                    playView?.updateView(currentIndex)
                } else {
                    playView?.play(currentIndex)
                }
            } else {
                playView?.play(index)
            }
        } else {
            if (hasEnsureNetStatus) {
142
                if (AudioPlayer.get().isPlaying && (from == 1 || from == 2)) {
严久程 committed
143 144 145 146 147 148 149 150 151 152 153
                    if (TextUtils.equals(playView?.getCurrentUrl(), list[currentIndex].url)) {
                        playView?.updateView(currentIndex)
                    } else {
                        playView?.play(currentIndex)
                    }
                } else {
                    playView?.play(currentIndex)
                }
            } else {
                playView?.showNetNotice {
                    hasEnsureNetStatus = true
154
                    if (AudioPlayer.get().isPlaying && (from == 1 || from == 2)) {
严久程 committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168
                        if (TextUtils.equals(playView?.getCurrentUrl(), list[currentIndex].url)) {
                            playView?.updateView(currentIndex)
                        } else {
                            playView?.play(currentIndex)
                        }
                    } else {
                        playView?.play(currentIndex)
                    }
                }
            }
        }
    }

    //将列表转为播放器可用列表
169 170
    private fun stringToMusicPlayerList(list: List<CourseMediaBean>): ArrayList<Music> {
        var nlist = ArrayList<Music>()
严久程 committed
171 172
        try {
            for (i in list.indices) {
173 174 175
                var be = Music()
                be.path = list[i].url
                be.coverPath = courseExtra!!.pic
严久程 committed
176
                be.title = list[i].title
177
                be.artist = list[i].doctorName
严久程 committed
178 179 180 181 182 183 184 185 186 187
                nlist.add(be)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return nlist
    }

    override fun onDestroy() {
        //释放播放器
188
        if (AudioPlayer.get().isPlaying) {
严久程 committed
189 190
            playView?.onDestroy()
            playView = null
191
            AudioPlayer.get().stopPlayer()
严久程 committed
192 193 194 195 196 197
        } else {
            playView?.onDestroy()
            playView = null
        }
    }
}