HPlayView.kt 8.35 KB
Newer Older
严久程 committed
1 2
package com.yidianling.course.widget

严久程 committed
3
import android.annotation.SuppressLint
严久程 committed
4
import android.app.Activity
konghaorui committed
5
import android.os.Build
严久程 committed
6
import android.os.Handler
YKai committed
7 8 9
import androidx.annotation.DrawableRes
import androidx.annotation.Nullable
import androidx.annotation.RawRes
严久程 committed
10
import android.view.View
konghaorui committed
11
import android.widget.ImageView
严久程 committed
12 13 14
import android.widget.RelativeLayout
import android.widget.SeekBar
import com.bumptech.glide.Glide
15 16 17 18 19 20 21
import com.ydl.media.audio.AudioPlayer
import com.ydl.media.audio.OnPlayerEventListener
import com.ydl.media.audio.enums.PlayModeEnum
import com.ydl.media.audio.model.Music
import com.ydl.media.view.PlayTypeEnum
import com.ydl.media.view.PlayerFloatHelper
import com.yidianling.course.listener.HPlayStatusListener
严久程 committed
22 23
import kotlinx.android.synthetic.main.course_play_music_view.view.*

konghaorui committed
24 25


严久程 committed
26 27 28
/**
 * Created by hgw on 2018/4/28.
 */
29
class HPlayView : RelativeLayout, OnPlayerEventListener {
严久程 committed
30
    var mHandler: Handler? = null
31
    var listener: HPlayStatusListener? = null
严久程 committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45
    //seekBar 按下标记
    var seekBarIsDown = false
    var progress = 0
    var mContext: Activity? = null

    constructor(context1: Activity?) : super(context1) {
        this.mContext = context1
        mHandler = Handler()
        init()
    }


    fun init() {
        if (mContext == null) return
konghaorui committed
46
        View.inflate(context, com.yidianling.course.R.layout.course_play_music_view, this)
严久程 committed
47
        AudioPlayer.get().addOnPlayEventListener(this)
严久程 committed
48

49
        course_audio_play_icon.setOnClickListener {
50
            AudioPlayer.get().playPause()
严久程 committed
51 52
        }
        img_gif.setOnClickListener {
53 54
            if (!AudioPlayer.get().isPlaying) {
                AudioPlayer.get().playPause()
严久程 committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
            }
        }

        pro_progress.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                if (p2) {
                    text_start_time.text = getStringTime(p1)
                    progress = p1
                }
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {
                seekBarIsDown = true
            }

            override fun onStopTrackingTouch(p0: SeekBar?) {
                seekBarIsDown = false
                //拖动seekbar时不进行以下操作
                if (!seekBarIsDown) {
74
                    AudioPlayer.get().seekTo(-1, progress.toLong())
严久程 committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
                }
            }
        })
    }


    fun showNetNotice(event: () -> Unit) {
        ll_net_notice.visibility = View.VISIBLE
        img_gif.visibility = View.GONE
        iv_net_notice.setOnClickListener {
            hideNetNotice()
            event()
        }
    }

    fun hideNetNotice() {
        ll_net_notice.visibility = View.GONE
        img_gif.visibility = View.VISIBLE
    }

95 96
    fun setData(urlLi: ArrayList<Music>): HPlayView {
        AudioPlayer.get().addPlayList(urlLi)
严久程 committed
97 98 99 100
        return this
    }

    fun updateView(index: Int) {
101
        if (AudioPlayer.get().isPlaying) {
严久程 committed
102
            mHandler?.postDelayed({
103
                pro_progress.max = AudioPlayer.get().getDuration().toInt()
严久程 committed
104
                text_end_time.text = getStringTime(pro_progress.max)
105
                listener?.onPrepared(AudioPlayer.get().playMusic, index)
严久程 committed
106 107 108 109 110 111 112 113 114
            }, 0)
            updateButton()
        }
    }

    /**
     * 获取当前播放url
     */
    fun getCurrentUrl(): String {
115
        return AudioPlayer.get().playMusic?.path ?: ""
严久程 committed
116 117 118
    }

    fun play(index: Int) {
严久程 committed
119
        PlayerFloatHelper.playingType = PlayTypeEnum.PLAY_TYPE_COURSE
120
        AudioPlayer.get().play(index)
严久程 committed
121
        updateButton()
严久程 committed
122 123 124
    }

    fun setImageBackground(url: String?) {
konghaorui committed
125
        if (!isDestroy()){
126
            mContext?.let { Glide.with(it).load(url).into(img_bg) }
konghaorui committed
127
        }
严久程 committed
128 129 130 131 132 133
    }

    /**
     * 设置自动播放下一曲
     */
    fun setAutoNext(auto: Boolean) {
严久程 committed
134 135 136 137
        if (!auto) {
            AudioPlayer.get().playMode = PlayModeEnum.SINGLE
        } else {
            AudioPlayer.get().playMode = PlayModeEnum.LIST_LOOP
138
        }
严久程 committed
139 140 141
    }


严久程 committed
142
    @SuppressLint("SetTextI18n")
143
    override fun onChange(music: Music) {
严久程 committed
144
        if (mContext != null) {
konghaorui committed
145
            displayImage(com.yidianling.course.R.drawable.course_loading5,img_gif,true)
严久程 committed
146
        }
konghaorui committed
147
        course_audio_play_icon.setImageResource(com.yidianling.course.R.drawable.course_ico_course_play)
严久程 committed
148 149 150

        pro_progress.progress = 0
        text_start_time.text = "00:00"
151
    }
严久程 committed
152

153 154 155
    override fun onPlayerStart() {
        setGifVisibity(true)
    }
严久程 committed
156

157
    override fun onPlayerPause() {
严久程 committed
158
        if (AudioPlayer.get().isPlaying) {
159
            setGifVisibity(true)
严久程 committed
160
        } else {
161 162 163
            setGifVisibity(false)
        }
    }
严久程 committed
164

165
    override fun onPublish(percent: Int, currentPosition: Long) {
严久程 committed
166 167 168 169
        if (!seekBarIsDown) {
            pro_progress.progress = currentPosition.toInt()
            text_start_time.text = getStringTime(currentPosition.toInt())
        }
170
    }
严久程 committed
171

172 173 174 175 176 177
    override fun onBufferingUpdate(percent: Int) {
        showBufferLoading(true)
        mHandler?.postDelayed({
            pro_progress.secondaryProgress = percent * pro_progress.max / 100
        }, 0)
    }
严久程 committed
178

179 180 181 182
    override fun onPrepared(duration: Long) {
        mHandler?.postDelayed({
            pro_progress.max = duration.toInt()
            text_end_time.text = getStringTime(pro_progress.max)
严久程 committed
183
            var index = AudioPlayer.get().getMusicList()?.indexOf(AudioPlayer.get().playMusic) ?: 0
184 185 186 187
            listener?.onPrepared(
                AudioPlayer.get().playMusic, index
            )
        }, 0)
严久程 committed
188

189
    }
严久程 committed
190

191 192
    override fun onComplete() {
        updateButton()
严久程 committed
193 194
    }

195 196

    private fun showBufferLoading(show: Boolean) {
严久程 committed
197 198
        mHandler?.postDelayed({
            if (show) {
严久程 committed
199 200
                if (!AudioPlayer.get().isPlaying) {
                    if (mContext != null) {
konghaorui committed
201
                        displayImage(com.yidianling.course.R.drawable.course_loading5,img_gif,true)
严久程 committed
202
                    }
konghaorui committed
203
                    course_audio_play_icon.setImageResource(com.yidianling.course.R.drawable.course_ico_course_play)
严久程 committed
204 205 206
                }
            } else {
                if (mContext != null) {
konghaorui committed
207
                    displayImage(com.yidianling.course.R.drawable.course_audio_play,img_gif,true)
严久程 committed
208
                }
konghaorui committed
209
                course_audio_play_icon.setImageResource(com.yidianling.course.R.drawable.course_ico_course_pause)
严久程 committed
210 211 212 213 214
            }
        }, 0)
    }

    //显示或隐藏播放动画
215
    private fun setGifVisibity(show: Boolean) {
严久程 committed
216
        if (mContext == null) return
严久程 committed
217 218
        mHandler?.postDelayed({
            if (show) {
konghaorui committed
219 220
                displayImage(com.yidianling.course.R.drawable.course_audio_play,img_gif,true)
                course_audio_play_icon.setImageResource(com.yidianling.course.R.drawable.course_ico_course_pause)
严久程 committed
221
            } else {
konghaorui committed
222 223
                displayImage(com.yidianling.course.R.drawable.course_ico_course_bg_pause,img_gif)
                course_audio_play_icon.setImageResource(com.yidianling.course.R.drawable.course_ico_course_play)
严久程 committed
224 225 226 227 228 229
            }
        }, 0)
    }

    //跟新上下音频按钮状态

230
    private fun updateButton() {
严久程 committed
231
        mHandler?.postDelayed({
232
            if (AudioPlayer.get().isPlaying) {
严久程 committed
233 234
                setGifVisibity(true)
            }
严久程 committed
235
        }, 300)
严久程 committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

    }

    fun getStringTime(time: Int): String {
        if (time <= 0) return "00:00"
        var min = time / 60000
        var ss = (time - min * 60000) / 1000

        var mm = "" + min
        var SS = "" + ss
        if (min < 10) {
            mm = "0$mm"
        }
        if (ss < 10) {
            SS = "0$ss"
        }
        return "$mm:$SS"
    }

    fun onDestroy() {
严久程 committed
256
        AudioPlayer.get().removeOnPlayEventListener(this)
严久程 committed
257 258 259
        mContext = null
        mHandler = null
    }
konghaorui committed
260 261 262 263 264 265 266 267

    /**
     * 加载Image
     */
    private fun displayImage(@RawRes @DrawableRes @Nullable  resourceId: Int, imageView: ImageView , isGif:Boolean = false) {
        //判断当前页面是否销毁
        if (!isDestroy()) {
            if(isGif){
268
                mContext?.let { Glide.with(it).asGif().load(resourceId).into(imageView) }
konghaorui committed
269
            }else {
270
                mContext?.let { Glide.with(it).asBitmap().load(resourceId).into(imageView) }
konghaorui committed
271 272 273 274 275 276 277 278 279 280 281 282
            }
        }
    }

    /**
     * 判断Activity是否Destroy
     * @param activity
     * @return
     */
    fun isDestroy(): Boolean {
        return mContext == null || mContext!!.isFinishing || Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mContext!!.isDestroyed
    }
严久程 committed
283
}