PlayerFloatView.kt 16.6 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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
package com.ydl.media.view

import android.annotation.SuppressLint
import android.content.Context
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.view.WindowManager
import android.widget.*
import com.ydl.media.R
import com.ydl.media.audio.AudioPlayer
import com.ydl.media.audio.OnPlayerEventListener
import com.ydl.media.audio.model.Music
import com.ydl.ydl_image.module.GlideApp
import com.ydl.ydl_image.transform.GlideRoundTransform
import java.util.*
import java.util.concurrent.CopyOnWriteArraySet


class PlayerFloatView(var mContext: Context) : FrameLayout(mContext) {
    private var mTouchStartX: Float = 0.toFloat()
    private var mTouchStartY: Float = 0.toFloat()
    private var mCurrX: Float = 0.toFloat()
    private var mCurrY: Float = 0.toFloat()

    private var playState: ImageView? = null
    private var mAudioLayout: LinearLayout? = null
    private var mInfoLayout: RelativeLayout? = null
    private var playClose: ImageView? = null
    private var playHead: ImageView? = null
    private var titleView: TextView? = null
    private var nameView: TextView? = null
    private var currentPlayingTimeView: TextView? = null
    private var durationView: TextView? = null
    private var mTouchSlop: Int = 0

    private var time: Int = 0
    private var allTime: Int = 0
    private var isMove: Boolean = false

    private var wm: WindowManager? = null
    //此wmParams为获取的全局变量,用以保存悬浮窗口的属性
    var wmParams = WindowManager.LayoutParams()

    private var myHandler: Handler? = null

    private val UPDATE_VIEW_STATE = 1
    private val UPDATE_VIEW_COMPLETE = 2
    private val UPDATE_PLAY_TIME = 3//更新播放时间

    private var listeners: CopyOnWriteArraySet<FloatViewPlayListener> = CopyOnWriteArraySet()
    private var mStateChangeListener: OnPlayerEventListener? = null
严久程 committed
57
    private var playData: HashMap<String, String> = hashMapOf<String, String>()
konghaorui committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    init {
        init(context)
    }

    fun resetWm(context: Context) {
        wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        wmParams = WindowManager.LayoutParams()
    }

    fun resetView() {
        resetAudioView()
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun init(context: Context) {
        wm = getContext().getSystemService(Context.WINDOW_SERVICE) as WindowManager
严久程 committed
75
        mTouchSlop = (ViewConfiguration.get(getContext()).scaledTouchSlop + 8) * 3
konghaorui committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        val view = View.inflate(context, R.layout.item_playing_float_btn, this)
        mAudioLayout = view.findViewById(R.id.ll_audio_layout)
        mInfoLayout = view.findViewById(R.id.rl_info)
        playHead = view.findViewById(R.id.play_head)
        playState = view.findViewById(R.id.play_state)
        playClose = view.findViewById(R.id.play_close)
        titleView = view.findViewById(R.id.tv_title)
        nameView = view.findViewById(R.id.tv_name)
        currentPlayingTimeView = view.findViewById(R.id.tv_now_playing_time)
        durationView = view.findViewById(R.id.tv_duration)

        resetAudioView()

        if (!TextUtils.isEmpty(AudioPlayer.get().playMusic?.coverPath)) {
            GlideApp.with(context.applicationContext)
严久程 committed
91 92 93 94
                .load(AudioPlayer.get().playMusic?.coverPath)
                .transform(GlideRoundTransform(context, 4))
                .error(R.drawable.ico_play_float_pic)
                .into(playHead!!)
konghaorui committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        }

        myHandler = MyHandler()

        playHead!!.setOnTouchListener { _, event ->
            mCurrX = event.rawX
            mCurrY = event.rawY - 25
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    //获取相对View的坐标,即以此View左上角为原点
                    mTouchStartX = event.x
                    mTouchStartY = event.y
                }
                MotionEvent.ACTION_MOVE -> {
                    if (Math.abs(event.x - mTouchStartX) > mTouchSlop || Math.abs(event.y - mTouchStartY) > mTouchSlop) {
                        updateViewPosition()
                        isMove = true
                    }
                }
                MotionEvent.ACTION_UP -> {
                    if (Math.abs(event.x - mTouchStartX) > mTouchSlop ||
严久程 committed
116 117
                        Math.abs(event.y - mTouchStartY) > mTouchSlop || isMove
                    ) {
konghaorui committed
118 119 120
                        updateViewPosition()
                    } else {
                        if (!isMove) {
严久程 committed
121
                            if (PlayerFloatHelper.isCanClick) {
严久程 committed
122 123
                                PlayerFloatHelper.startPlayingActivity(context)
                            }
konghaorui committed
124 125 126 127 128 129 130 131 132 133
                        }
                    }
                    isMove = false
                    mTouchStartY = 0f
                    mTouchStartX = mTouchStartY
                }
            }
            true
        }

徐健 committed
134
        titleView!!.setOnTouchListener { _, event ->
konghaorui committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
            mCurrX = event.rawX
            mCurrY = event.rawY - 25
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    //获取相对View的坐标,即以此View左上角为原点
                    mTouchStartX = event.x
                    mTouchStartY = event.y
                }
                MotionEvent.ACTION_MOVE -> {
                    if (Math.abs(event.x - mTouchStartX) > mTouchSlop || Math.abs(event.y - mTouchStartY) > mTouchSlop) {
                        updateViewPosition()
                        isMove = true
                    }
                }
                MotionEvent.ACTION_UP -> {
                    if (Math.abs(event.x - mTouchStartX) > mTouchSlop ||
严久程 committed
151 152
                        Math.abs(event.y - mTouchStartY) > mTouchSlop || isMove
                    ) {
konghaorui committed
153 154 155
                        updateViewPosition()
                    } else {
                        if (!isMove) {
严久程 committed
156
                            if (PlayerFloatHelper.isCanClick) {
严久程 committed
157 158
                                PlayerFloatHelper.startPlayingActivity(context)
                            }
konghaorui committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
                        }
                    }
                    isMove = false
                    mTouchStartY = 0f
                    mTouchStartX = mTouchStartY
                }
            }
            true
        }

        nameView!!.setOnTouchListener { _, event ->
            mCurrX = event.rawX
            mCurrY = event.rawY - 25
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    //获取相对View的坐标,即以此View左上角为原点
                    mTouchStartX = event.x
                    mTouchStartY = event.y
                }
                MotionEvent.ACTION_MOVE -> {
                    if (Math.abs(event.x - mTouchStartX) > mTouchSlop || Math.abs(event.y - mTouchStartY) > mTouchSlop) {
                        updateViewPosition()
                        isMove = true
                    }
                }
                MotionEvent.ACTION_UP -> {
                    if (Math.abs(event.x - mTouchStartX) > mTouchSlop ||
严久程 committed
186 187
                        Math.abs(event.y - mTouchStartY) > mTouchSlop || isMove
                    ) {
konghaorui committed
188 189 190
                        updateViewPosition()
                    } else {
                        if (!isMove) {
严久程 committed
191
                            if (PlayerFloatHelper.isCanClick) {
严久程 committed
192 193
                                PlayerFloatHelper.startPlayingActivity(context)
                            }
konghaorui committed
194 195 196 197 198 199 200 201 202 203
                        }
                    }
                    isMove = false
                    mTouchStartY = 0f
                    mTouchStartX = mTouchStartY
                }
            }
            true
        }

徐健 committed
204
        playClose!!.setOnTouchListener { _, event ->
konghaorui committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
            mCurrX = event.rawX
            mCurrY = event.rawY - 25
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    //获取相对View的坐标,即以此View左上角为原点
                    mTouchStartX = event.x
                    mTouchStartY = event.y
                }
                MotionEvent.ACTION_MOVE -> {
                    if (Math.abs(event.x - mTouchStartX) > mTouchSlop || Math.abs(event.y - mTouchStartY) > mTouchSlop) {
                        updateViewPosition()
                        isMove = true
                    }
                }
                MotionEvent.ACTION_UP -> {
                    if (Math.abs(event.x - mTouchStartX) > mTouchSlop ||
严久程 committed
221 222
                        Math.abs(event.y - mTouchStartY) > mTouchSlop || isMove
                    ) {
konghaorui committed
223 224 225
                        updateViewPosition()
                    } else {
                        if (!isMove) {
严久程 committed
226 227 228 229 230
                            for (listener in listeners) {
                                //数据重新设置回调
                                listener.onPauseClick()
                            }

konghaorui committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
                            this@PlayerFloatView.visibility = View.GONE
                            PlayerFloatHelper.removeView(mContext)
                            PlayerFloatHelper.playTempData.clear()
                            AudioPlayer.get().stopPlayer()
                        }
                    }
                    isMove = false
                    mTouchStartY = 0f
                    mTouchStartX = mTouchStartY
                }
            }
            true
        }

        playState!!.setOnTouchListener { _, event ->
            mCurrX = event.rawX
            mCurrY = event.rawY - 25
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    //获取相对View的坐标,即以此View左上角为原点
                    mTouchStartX = event.x
                    mTouchStartY = event.y
                }
                MotionEvent.ACTION_MOVE -> {
                    if (Math.abs(event.x - mTouchStartX) > mTouchSlop || Math.abs(event.y - mTouchStartY) > mTouchSlop) {
                        updateViewPosition()
                        isMove = true
                    }
                }
                MotionEvent.ACTION_UP -> {
                    if (Math.abs(event.x - mTouchStartX) > mTouchSlop ||
严久程 committed
262 263
                        Math.abs(event.y - mTouchStartY) > mTouchSlop || isMove
                    ) {
konghaorui committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
                        updateViewPosition()
                    } else {
                        if (!isMove) {
                            if (AudioPlayer.get().isPlaying) {
                                playState!!.setImageResource(R.drawable.ico_yyfc_play)
                                AudioPlayer.get().playPause()
                                for (listener in listeners) {
                                    //数据重新设置回调
                                    listener.onPauseClick()
                                }
                            } else {
                                playState!!.setImageResource(R.drawable.ico_yyfc_pause)
                                AudioPlayer.get().playPause()
                                for (listener in listeners) {
                                    //数据重新设置回调
                                    listener.onStartClick()
                                }
                            }
                        }
                    }
                    isMove = false
                    mTouchStartY = 0f
                    mTouchStartX = mTouchStartY
                }
            }
            true
        }

        if (mStateChangeListener == null) {
严久程 committed
293
            mStateChangeListener = object : OnPlayerEventListener {
konghaorui committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
                override fun onComplete() {
                    myHandler?.sendEmptyMessage(UPDATE_VIEW_COMPLETE)
                }

                override fun onChange(music: Music) {
                }

                override fun onPlayerStart() {
                }

                override fun onPlayerPause() {
                }

                override fun onPublish(percent: Int, currentPosition: Long) {
                    this@PlayerFloatView.time = currentPosition.toInt()
                    myHandler?.sendEmptyMessage(UPDATE_PLAY_TIME)
                }

                override fun onBufferingUpdate(percent: Int) {
                }

                override fun onPrepared(duration: Long) {
                    this@PlayerFloatView.allTime = duration.toInt()
                    myHandler?.sendEmptyMessage(UPDATE_PLAY_TIME)
                    myHandler?.sendEmptyMessage(UPDATE_VIEW_STATE)
                }
            }
            AudioPlayer.get().addOnPlayEventListener(mStateChangeListener!!)
        }
    }


    private fun resetAudioView() {
        if (!TextUtils.isEmpty(AudioPlayer.get().playMusic?.coverPath)) {
            GlideApp.with(context.applicationContext)
严久程 committed
329 330 331 332
                .load(AudioPlayer.get().playMusic?.coverPath)
                .transform(GlideRoundTransform(context, 4))
                .error(R.drawable.ico_play_float_pic)
                .into(playHead!!)
konghaorui committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
        }

        if (TextUtils.isEmpty(AudioPlayer.get().playMusic?.title)) {
            if (PlayerFloatHelper.playingType == PlayTypeEnum.PLAY_TYPE_FM) {
                titleView!!.text = "心灵电台"
            }
            if (PlayerFloatHelper.playingType == PlayTypeEnum.PLAY_TYPE_CONFIDE) {
                titleView!!.text = "壹点倾诉,心灵寄语"
            }
        } else {
            titleView!!.text = AudioPlayer.get().playMusic?.title
        }
        if (TextUtils.isEmpty(AudioPlayer.get().playMusic?.artist)) {
            nameView!!.text = "壹点灵"
        } else {
            nameView!!.text = AudioPlayer.get().playMusic?.artist
        }
        mAudioLayout!!.visibility = View.VISIBLE

    }


    private 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"
    }

    private fun updatePlayTime() {
        currentPlayingTimeView?.text = getStringTime(time)
        durationView?.text = getStringTime(allTime)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        //获取相对屏幕的坐标,即以屏幕左上角为原点
        mCurrX = event.rawX
        mCurrY = event.rawY - 25
        Log.i("currP", "currX$mCurrX====currY$mCurrY")
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                //获取相对View的坐标,即以此View左上角为原点
                mTouchStartX = event.x
                mTouchStartY = event.y
            }
            MotionEvent.ACTION_MOVE -> updateViewPosition()
            MotionEvent.ACTION_UP -> {
                updateViewPosition()
                mTouchStartY = 0f
                mTouchStartX = mTouchStartY
            }
        }
        return true
    }

    private fun updateViewPosition() {
        //更新浮动窗口位置参数
        wmParams.x = (mCurrX - mTouchStartX).toInt()
        wmParams.y = (mCurrY - mTouchStartY).toInt()
        wm!!.updateViewLayout(this, wmParams)
    }

    fun updatePlayState() {
        if (AudioPlayer.get().isPlaying) {
            playState!!.setImageResource(R.drawable.ico_yyfc_pause)
        } else {
            playState!!.setImageResource(R.drawable.ico_yyfc_play)
            for (listener in listeners) {
                listener.onPauseClick()
            }
        }
    }

    fun setPlayingState() {
        playState!!.setImageResource(R.drawable.ico_yyfc_pause)
        resetAudioView()
    }

    fun onDestroy() {
        listeners.clear()
        mStateChangeListener?.let { AudioPlayer.get().removeOnPlayEventListener(it) }
    }

    fun addFloatClickListener(floatClickListener: FloatViewPlayListener) {
        listeners.add(floatClickListener)
    }

    fun removeFloatClickListener(floatClickListener: FloatViewPlayListener) {
        listeners.remove(floatClickListener)
    }

    interface FloatViewPlayListener {

        fun onPauseClick()

        fun onStartClick()

        fun onPlayFinish()
    }

    internal inner class MyHandler : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                UPDATE_VIEW_STATE -> updatePlayState()
                UPDATE_VIEW_COMPLETE -> {
                    updatePlayState()
                    for (listener in listeners) {
                        //数据重新设置回调
                        listener.onPlayFinish()
                    }
                }
                UPDATE_PLAY_TIME -> {
                    updatePlayTime()
                }
            }
        }
    }
}