MeditationFloatWindow.kt 17.7 KB
Newer Older
范玉宾 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 116 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 148 149 150 151 152 153 154 155 156 157 158 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 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 262 263 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 293 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 329 330 331 332 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
package com.yidianling.muse.helper

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.*
import android.widget.*
import androidx.constraintlayout.widget.ConstraintLayout
import com.ydl.media.audio.AudioPlayer
import com.ydl.media.audio.OnPlayerEventListener
import com.ydl.media.audio.model.Music
import com.ydl.media.view.PlayTypeEnum
import com.ydl.media.view.PlayerFloatHelper
import com.ydl.ydl_image.module.GlideApp
import com.ydl.ydl_image.transform.GlideRoundTransform
import com.yidianling.muse.R
import kotlinx.android.synthetic.main.layout_meditation_play_float_view.view.*
import java.util.*
import java.util.concurrent.CopyOnWriteArraySet

class MeditationFloatWindow(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 mRootLayout: ConstraintLayout? = null
        private var playClose: ImageView? = null
        private var playHead: ImageView? = null
        private var titleView: TextView? = null

        private var llMeditationInfo: LinearLayout? = 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
        private var playData: HashMap<String, String> = hashMapOf<String, String>()

        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
            mTouchSlop = (ViewConfiguration.get(getContext()).scaledTouchSlop + 8) * 3
            val view = View.inflate(context, R.layout.layout_meditation_play_float_view, this)
            mRootLayout = view.findViewById(R.id.csl_root_layout)
            playState = view.findViewById(R.id.iv_play_status)
            playHead = view.findViewById(R.id.iv_cover)
            playClose = view.findViewById(R.id.iv_close)
            titleView = view.findViewById(R.id.tv_title)

            llMeditationInfo = view.findViewById(R.id.ll_meditation_info)

            resetAudioView()

            if (!TextUtils.isEmpty(AudioPlayer.get().playMusic?.coverPath)) {
                GlideApp.with(context.applicationContext)
                    .load(AudioPlayer.get().playMusic?.coverPath)
                    .transform(GlideRoundTransform(context, 4))
                    .error(com.ydl.media.R.drawable.ico_play_float_pic)
                    .into(playHead!!)
            }

            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 ||
                            Math.abs(event.y - mTouchStartY) > mTouchSlop || isMove
                        ) {
                            updateViewPosition()
                        } else {
                            if (!isMove) {
                                if (PlayerFloatHelper.isCanClick) {
                                    PlayerFloatHelper.startPlayingActivity(context)
                                }
                            }
                        }
                        isMove = false
                        mTouchStartY = 0f
                        mTouchStartX = mTouchStartY
                    }
                }
                true
            }

            titleView!!.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 ||
                            Math.abs(event.y - mTouchStartY) > mTouchSlop || isMove
                        ) {
                            updateViewPosition()
                        } else {
                            if (!isMove) {
                                if (PlayerFloatHelper.isCanClick) {
                                    PlayerFloatHelper.startPlayingActivity(context)
                                }
                            }
                        }
                        isMove = false
                        mTouchStartY = 0f
                        mTouchStartX = mTouchStartY
                    }
                }
                true
            }

            llMeditationInfo?.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 ||
                            Math.abs(event.y - mTouchStartY) > mTouchSlop || isMove
                        ) {
                            updateViewPosition()
                        } else {
                            if (!isMove) {
                                if (PlayerFloatHelper.isCanClick) {
                                    PlayerFloatHelper.startPlayingActivity(context)
                                }
                            }
                        }
                        isMove = false
                        mTouchStartY = 0f
                        mTouchStartX = mTouchStartY
                    }
                }
                true
            }

            playClose!!.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 ||
                            Math.abs(event.y - mTouchStartY) > mTouchSlop || isMove
                        ) {
                            updateViewPosition()
                        } else {
                            if (!isMove) {
                                for (listener in listeners) {
                                    //数据重新设置回调
                                    listener.onPauseClick()
                                }

                                this@MeditationFloatWindow.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 ||
                            Math.abs(event.y - mTouchStartY) > mTouchSlop || isMove
                        ) {
                            updateViewPosition()
                        } else {
                            if (!isMove) {
                                if (AudioPlayer.get().isPlaying) {
                                    playState!!.setImageResource(com.ydl.media.R.drawable.ico_yyfc_play)
                                    AudioPlayer.get().playPause()
                                    for (listener in listeners) {
                                        //数据重新设置回调
                                        listener.onPauseClick()
                                    }
                                } else {
                                    playState!!.setImageResource(com.ydl.media.R.drawable.ico_yyfc_pause)
                                    AudioPlayer.get().playPause()
                                    for (listener in listeners) {
                                        //数据重新设置回调
                                        listener.onStartClick()
                                    }
                                }
                            }
                        }
                        isMove = false
                        mTouchStartY = 0f
                        mTouchStartX = mTouchStartY
                    }
                }
                true
            }

            if (mStateChangeListener == null) {
                mStateChangeListener = object : OnPlayerEventListener {
                    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@MeditationFloatWindow.time = currentPosition.toInt()
                        myHandler?.sendEmptyMessage(UPDATE_PLAY_TIME)
                    }

                    override fun onBufferingUpdate(percent: Int) {
                    }

                    override fun onPrepared(duration: Long) {
                        this@MeditationFloatWindow.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)
                    .load(AudioPlayer.get().playMusic?.coverPath)
                    .transform(GlideRoundTransform(context, 4))
                    .error(com.ydl.media.R.drawable.ico_play_float_pic)
                    .into(playHead!!)
            }

            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
            }

            mRootLayout!!.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() {

        }

        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(com.ydl.media.R.drawable.ico_yyfc_pause)
            } else {
                playState!!.setImageResource(com.ydl.media.R.drawable.ico_yyfc_play)
                for (listener in listeners) {
                    listener.onPauseClick()
                }
            }
        }

        fun setPlayingState() {
            playState!!.setImageResource(com.ydl.media.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()
                    }
                }
            }
        }
    }