PlayerFloatHelper.kt 7.41 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7 8
package com.ydl.media.view

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.PixelFormat
import android.graphics.Point
import android.os.Bundle
YKai committed
9
import androidx.core.view.ViewCompat
konghaorui committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import com.alibaba.android.arouter.launcher.ARouter
import com.ydl.media.audio.AudioPlayer
import com.yidianling.common.tools.RxImageTool

/**
 * 音频播放悬浮窗 帮助类
 *
 * 需要在Activity onDestroy中调用removeView()方法移除
 * 避免内存泄露
 */
class PlayerFloatHelper {

    companion object {
        @SuppressLint("StaticFieldLeak")
        private var mPlayerFloatView: PlayerFloatView? = null
        //已添加悬浮窗页面全路径名
        private var showingPageName: String? = null
        private var wm: WindowManager? = null
严久程 committed
32
        var isCanClick = true
konghaorui committed
33 34 35

        var playingType = PlayTypeEnum.PLAY_TYPE_NONE
        var playTempData = hashMapOf<String,String>()
徐健 committed
36 37 38 39 40 41 42
        /**
         * FM:
         * fmId
         * fmTitle
         * fmAuthor
         * fmImageUrl
         */
konghaorui committed
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

        /**
         * 如果有音频正在播放则显示
         */
        fun showIfPlaying(context: Context) {
            if (AudioPlayer.get().isPlaying) {
                show(context)
                mPlayerFloatView?.updatePlayState()
            } else {
                hide()
            }
        }

        /**
         * 显示悬浮控件
         */
        fun show(context: Context, playTypeEnum: PlayTypeEnum = PlayTypeEnum.PLAY_TYPE_NONE, playData:HashMap<String,String> = hashMapOf<String,String>()) {

            playingType = playTypeEnum
            if (playData.size>0){
                this.playTempData.putAll(playData)
            }

            if (mPlayerFloatView == null) {
                mPlayerFloatView = PlayerFloatView(context)
                mPlayerFloatView?.addFloatClickListener(object :PlayerFloatView.FloatViewPlayListener{
                    override fun onPauseClick() {

                    }

                    override fun onStartClick() {

                    }

                    override fun onPlayFinish() {

                    }

                })
            }

            if (showingPageName != context::class.qualifiedName) {
                mPlayerFloatView?.resetWm(context)
                addFloatToWm(context)
            }

            mPlayerFloatView?.resetView()

            mPlayerFloatView?.visibility = View.VISIBLE
            mPlayerFloatView?.setPlayingState()
        }

        fun hide() {
            mPlayerFloatView?.visibility = View.GONE
        }

        fun addClickListener(listener: PlayerFloatView.FloatViewPlayListener) {
            mPlayerFloatView?.addFloatClickListener(listener)
        }

        fun removeClickListener(listener: PlayerFloatView.FloatViewPlayListener) {
            mPlayerFloatView?.removeFloatClickListener(listener)
        }

        fun isShow(context: Context): Boolean {
            return !TextUtils.isEmpty(showingPageName) && showingPageName == context::class.qualifiedName && mPlayerFloatView?.visibility == View.VISIBLE
        }

        fun removeView(context: Context) {
            if (TextUtils.isEmpty(showingPageName) || showingPageName != context::class.qualifiedName) {
                return
            }
            mPlayerFloatView?.visibility = View.GONE
            wm?.removeViewImmediate(mPlayerFloatView)
            showingPageName = ""
            wm = null
        }

        fun onDestroy() {
            if (mPlayerFloatView != null) {
                if (!TextUtils.isEmpty(showingPageName)) {
                    wm?.removeViewImmediate(mPlayerFloatView)
                    showingPageName = ""
                }

                mPlayerFloatView?.onDestroy()
                mPlayerFloatView?.removeAllViews()

            }
        }

        fun setPlayingState(context: Context) {
            show(context)
            mPlayerFloatView?.setPlayingState()
        }

        fun updatePlayState() {
            mPlayerFloatView?.updatePlayState()
        }

        private fun addFloatToWm(context: Context) {
            if (wm != null && !TextUtils.isEmpty(showingPageName)) {
YKai committed
145
                if (ViewCompat.isAttachedToWindow(mPlayerFloatView!!)){
146 147 148 149 150
//                    if (context is Activity && !(context.isFinishing)){
//                      wm?.removeViewImmediate(mPlayerFloatView)
//                    }
                    wm?.removeViewImmediate(mPlayerFloatView)
                }
konghaorui committed
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
                wm = null
            }
            //获取WindowManager
            wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            //设置LayoutParams(全局变量)相关参数
            val wmParams = mPlayerFloatView?.wmParams
            wmParams?.type = WindowManager.LayoutParams.TYPE_APPLICATION   //设置window type
            wmParams?.format = PixelFormat.RGBA_8888   //设置图片格式,效果为背景透明
            //设置Window flag
            wmParams?.flags =
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            wmParams?.gravity = Gravity.LEFT or Gravity.TOP   //调整悬浮窗口至左上角
            //以屏幕左上角为原点,设置x、y初始值
            val size = Point()
            wm?.defaultDisplay?.getSize(size)
            wmParams?.x = 0
            wmParams?.y = size.y * 5 / 6
            //设置悬浮窗口长宽数据
            wmParams?.width = WindowManager.LayoutParams.MATCH_PARENT
            wmParams?.height = RxImageTool.dp2px(56f)
            //显示myFloatView图像
            wm?.addView(mPlayerFloatView, wmParams)
            showingPageName = context::class.qualifiedName!!
        }

        /**
         * 打开播放中的页面详情
         */
        fun startPlayingActivity(context: Context?, fullScreen: Int = 0) {
            if (playingType == PlayTypeEnum.PLAY_TYPE_FM) {
                //FM播放页
                startFMPlayActivity(context)
            } else if (playingType == PlayTypeEnum.PLAY_TYPE_COURSE) {
                var url = AudioPlayer.get().playMusic?.path
                //课程播放页
                startCoursePlayActivity(context, 1, fullScreen, url, true)
            }
        }

        fun startCoursePlayActivity(context: Context?, from: Int, fullScreen: Int = 0, coursePlayUrl: String? = "", isFromFloatView: Boolean = false) {
            ARouter.getInstance()
                .build("/course/play")
                .withInt("course_id", playTempData["course_id"]?.toInt()?:0)
                .withInt("course_type", 0)
                .withString("coursePlayUrl", coursePlayUrl)
                .withInt("from", from)
                .withBoolean("isFromFloatView", isFromFloatView)
                .withInt("fullScreen", fullScreen)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .navigation()
        }

        fun startFMPlayActivity(context: Context?) {
            val bundle = Bundle()
            bundle.putInt("id", playTempData["fmId"]?.toInt()?:0)
            ARouter.getInstance().build("/fm/detail")
                .withBundle("bundle", bundle)
                .navigation()
        }

徐健 committed
211 212 213 214 215 216 217 218 219
        /**
         * 获取FM播放Id
         */
        fun getFmId(): Int {
            return playTempData["fmId"]?.toInt()?:0
        }



konghaorui committed
220 221 222 223

    }

}