Commit fc94ddc1 by 王佳洋

修复一些悬浮窗线上存在的已知bug

如悬浮窗代码优化,不再只是为了结果而结果,如悬浮窗的可见性
如悬浮窗导致NewH5Activity上下文无法释放
如悬浮窗播放完当前音频之后,播放下一音频时,显式不正确
如悬浮窗跟随手势滑动异常
如悬浮窗关闭,但音频播放未停止
如悬浮窗跟随手势滑动后进入其他页面后再次回到初始位置
parent b818b158
...@@ -151,7 +151,7 @@ public class PlayFragment extends Fragment implements View.OnClickListener, ...@@ -151,7 +151,7 @@ public class PlayFragment extends Fragment implements View.OnClickListener,
} }
private void showFloatView() { private void showFloatView() {
if(!PlayerFloatHelper.Companion.isShow(getActivity())) { if(!PlayerFloatHelper.Companion.isShow()) {
PlayerFloatHelper.Companion.show(getActivity(), PlayTypeEnum.PLAY_TYPE_FM,new HashMap<>()); PlayerFloatHelper.Companion.show(getActivity(), PlayTypeEnum.PLAY_TYPE_FM,new HashMap<>());
}else { }else {
PlayerFloatHelper.Companion.showIfPlaying(getActivity()); PlayerFloatHelper.Companion.showIfPlaying(getActivity());
...@@ -267,7 +267,7 @@ public class PlayFragment extends Fragment implements View.OnClickListener, ...@@ -267,7 +267,7 @@ public class PlayFragment extends Fragment implements View.OnClickListener,
@Override @Override
public void onDestroy() { public void onDestroy() {
AudioPlayer.Companion.get().removeOnPlayEventListener(this); AudioPlayer.Companion.get().removeOnPlayEventListener(this);
PlayerFloatHelper.Companion.onDestroy(); PlayerFloatHelper.Companion.removeResetView();
super.onDestroy(); super.onDestroy();
} }
......
...@@ -184,9 +184,9 @@ class ConfideHomeEventImpl(context: Context, var confideHomeView: IConfideHomeCo ...@@ -184,9 +184,9 @@ class ConfideHomeEventImpl(context: Context, var confideHomeView: IConfideHomeCo
mu.path = playUrl mu.path = playUrl
mu.coverPath=confideIcon mu.coverPath=confideIcon
AudioPlayer.get().singlePlay(mu) AudioPlayer.get().singlePlay(mu)
if (PlayerFloatHelper.isShow(mContext!!)) { if (PlayerFloatHelper.isShow()) {
if (AudioPlayer.get().playMode.value() == 1) { if (AudioPlayer.get().playMode.value() == 1) {
PlayerFloatHelper.removeView(mContext!!) PlayerFloatHelper.removeView()
PlayerFloatHelper.show(mContext!!) PlayerFloatHelper.show(mContext!!)
} else { } else {
...@@ -270,9 +270,7 @@ class ConfideHomeEventImpl(context: Context, var confideHomeView: IConfideHomeCo ...@@ -270,9 +270,7 @@ class ConfideHomeEventImpl(context: Context, var confideHomeView: IConfideHomeCo
} }
override fun destoryPlayer() { override fun destoryPlayer() {
PlayerFloatHelper.hide() PlayerFloatHelper.removeResetView()
PlayerFloatHelper.playTempData = hashMapOf<String,String>()
AudioPlayer.get().resetPlayer()
floatViewClickListener?.let { PlayerFloatHelper.removeClickListener(it) }; floatViewClickListener?.let { PlayerFloatHelper.removeClickListener(it) };
} }
......
...@@ -60,6 +60,6 @@ public class CourseActivity extends BaseActivity { ...@@ -60,6 +60,6 @@ public class CourseActivity extends BaseActivity {
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
PlayerFloatHelper.Companion.onDestroy(); PlayerFloatHelper.Companion.removeResetView();
} }
} }
...@@ -738,9 +738,7 @@ class CourseListContainerActivity : BaseActivity(), PtrHandler, LoadMoreHandler ...@@ -738,9 +738,7 @@ class CourseListContainerActivity : BaseActivity(), PtrHandler, LoadMoreHandler
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
if (!PlayerFloatHelper.isCanClick) { if (!PlayerFloatHelper.isCanClick) {
PlayerFloatHelper.hide() PlayerFloatHelper.removeResetView()
PlayerFloatHelper.removeView(this)
AudioPlayer.get().resetPlayer()
} }
} }
......
...@@ -262,8 +262,8 @@ class AudioPlayActivity : BaseMvpActivity<IAudioPlayContract.View, IAudioPlayCon ...@@ -262,8 +262,8 @@ class AudioPlayActivity : BaseMvpActivity<IAudioPlayContract.View, IAudioPlayCon
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (PlayerFloatHelper.isShow(this)) { if (PlayerFloatHelper.isShow()) {
PlayerFloatHelper.removeView(this) PlayerFloatHelper.removeView()
} }
} }
......
...@@ -622,8 +622,8 @@ class CoursePlayActivity : BaseActivity() { ...@@ -622,8 +622,8 @@ class CoursePlayActivity : BaseActivity() {
playVideoView?.onResume() playVideoView?.onResume()
playAudioView?.onResume() playAudioView?.onResume()
if (PlayerFloatHelper.isShow(this)) { if (PlayerFloatHelper.isShow()) {
PlayerFloatHelper.removeView(this) PlayerFloatHelper.removeView()
} }
} }
......
...@@ -122,9 +122,7 @@ class CoursePlugin : MethodChannel.MethodCallHandler { ...@@ -122,9 +122,7 @@ class CoursePlugin : MethodChannel.MethodCallHandler {
if (jumpUrl!!.startsWith("http")) { if (jumpUrl!!.startsWith("http")) {
val h5Params = H5Params(jumpUrl, "") val h5Params = H5Params(jumpUrl, "")
NewH5Activity.start(mFragment!!.activity, h5Params) NewH5Activity.start(mFragment!!.activity, h5Params)
PlayerFloatHelper.hide() PlayerFloatHelper.removeResetView()
PlayerFloatHelper.removeView(mFragment!!.requireActivity())
AudioPlayer.get().resetPlayer()
CourseSendPlugin.sendMsg(false) CourseSendPlugin.sendMsg(false)
return return
} }
...@@ -164,9 +162,7 @@ class CoursePlugin : MethodChannel.MethodCallHandler { ...@@ -164,9 +162,7 @@ class CoursePlugin : MethodChannel.MethodCallHandler {
} }
else -> YDLRouterManager.router(jumpUrl) else -> YDLRouterManager.router(jumpUrl)
} }
PlayerFloatHelper.hide() PlayerFloatHelper.removeResetView()
PlayerFloatHelper.removeView(mFragment!!.requireActivity())
AudioPlayer.get().resetPlayer()
CourseSendPlugin.sendMsg(false) CourseSendPlugin.sendMsg(false)
} }
} }
...@@ -194,9 +190,7 @@ class CoursePlugin : MethodChannel.MethodCallHandler { ...@@ -194,9 +190,7 @@ class CoursePlugin : MethodChannel.MethodCallHandler {
EventBus.getDefault().post(ScrollStatusChangeEvent(true)) EventBus.getDefault().post(ScrollStatusChangeEvent(true))
} }
IOSPOP -> { IOSPOP -> {
PlayerFloatHelper.hide() PlayerFloatHelper.removeResetView()
PlayerFloatHelper.removeView(mFragment!!.requireActivity())
AudioPlayer.get().resetPlayer()
CourseSendPlugin.sendMsg(false) CourseSendPlugin.sendMsg(false)
mFragment!!.activity?.finish() mFragment!!.activity?.finish()
} }
...@@ -204,12 +198,10 @@ class CoursePlugin : MethodChannel.MethodCallHandler { ...@@ -204,12 +198,10 @@ class CoursePlugin : MethodChannel.MethodCallHandler {
} }
private fun playMedia(fileInfo: Map<*, *>) { private fun playMedia(fileInfo: Map<*, *>) {
if (PlayerFloatHelper.isShow(mFragment!!.requireActivity())) { if (PlayerFloatHelper.isShow()) {
PlayerFloatHelper.hide() PlayerFloatHelper.removeResetView()
PlayerFloatHelper.removeView(mFragment!!.requireActivity())
AudioPlayer.get().resetPlayer()
} else { } else {
PlayerFloatHelper.removeView(mFragment!!.requireActivity()) PlayerFloatHelper.removeView()
} }
...@@ -245,10 +237,8 @@ class CoursePlugin : MethodChannel.MethodCallHandler { ...@@ -245,10 +237,8 @@ class CoursePlugin : MethodChannel.MethodCallHandler {
) )
checkPermission() checkPermission()
if (PlayerFloatHelper.isShow(mFragment!!.requireActivity())) { if (PlayerFloatHelper.isShow()) {
PlayerFloatHelper.hide() PlayerFloatHelper.removeResetView()
PlayerFloatHelper.removeView(mFragment!!.requireActivity())
AudioPlayer.get().resetPlayer()
} }
} }
...@@ -262,8 +252,7 @@ class CoursePlugin : MethodChannel.MethodCallHandler { ...@@ -262,8 +252,7 @@ class CoursePlugin : MethodChannel.MethodCallHandler {
} }
override fun onPlayFinish() { override fun onPlayFinish() {
PlayerFloatHelper.hide() PlayerFloatHelper.removeResetView()
AudioPlayer.get().resetPlayer()
CourseSendPlugin.sendMsg(false) CourseSendPlugin.sendMsg(false)
} }
......
...@@ -22,9 +22,7 @@ class CoursePlayLifecycle : Application.ActivityLifecycleCallbacks { ...@@ -22,9 +22,7 @@ class CoursePlayLifecycle : Application.ActivityLifecycleCallbacks {
override fun onActivityResumed(activity: Activity?) { override fun onActivityResumed(activity: Activity?) {
if (!PlayerFloatHelper.isCanClick) { if (!PlayerFloatHelper.isCanClick) {
PlayerFloatHelper.hide() PlayerFloatHelper.removeResetView()
PlayerFloatHelper.removeView(activity!!)
AudioPlayer.get().resetPlayer()
} else { } else {
PlayerFloatHelper.showIfPlaying(activity!!) PlayerFloatHelper.showIfPlaying(activity!!)
if (!TextUtils.isEmpty(PlayerFloatHelper.playTempData["course_id"])) { if (!TextUtils.isEmpty(PlayerFloatHelper.playTempData["course_id"])) {
......
...@@ -8,6 +8,7 @@ import com.alibaba.android.arouter.facade.annotation.Route ...@@ -8,6 +8,7 @@ import com.alibaba.android.arouter.facade.annotation.Route
import com.ydl.course.api.ICourseService import com.ydl.course.api.ICourseService
import com.ydl.media.audio.AudioPlayer import com.ydl.media.audio.AudioPlayer
import com.ydl.media.view.PlayerFloatHelper import com.ydl.media.view.PlayerFloatHelper
import com.yidianling.course.bean.COURSE_AUDIO
import com.yidianling.course.courseNew.CourseTopicActivity import com.yidianling.course.courseNew.CourseTopicActivity
import com.yidianling.course.courseNew.mine.MyCourseActivity import com.yidianling.course.courseNew.mine.MyCourseActivity
import com.yidianling.course.coursePlay.AudioPlayActivity import com.yidianling.course.coursePlay.AudioPlayActivity
...@@ -67,7 +68,6 @@ class CourseServiceImp : ICourseService { ...@@ -67,7 +68,6 @@ class CourseServiceImp : ICourseService {
// Intent(activity, CoursePlayActivity::class.java) // Intent(activity, CoursePlayActivity::class.java)
// } // }
val intent = Intent(activity, AudioPlayActivity::class.java) val intent = Intent(activity, AudioPlayActivity::class.java)
intent.putExtra("course_id", courseId) intent.putExtra("course_id", courseId)
intent.putExtra("coursePlayUrl", coursePlayUrl) intent.putExtra("coursePlayUrl", coursePlayUrl)
intent.putExtra("from", from) intent.putExtra("from", from)
......
...@@ -273,12 +273,10 @@ class CourseItemNewView : ConstraintLayout { ...@@ -273,12 +273,10 @@ class CourseItemNewView : ConstraintLayout {
private fun playMedia() { private fun playMedia() {
if (PlayerFloatHelper.isShow(mContext)) { if (PlayerFloatHelper.isShow()) {
PlayerFloatHelper.hide() PlayerFloatHelper.removeResetView()
PlayerFloatHelper.removeView(mContext)
AudioPlayer.get().resetPlayer()
} else { } else {
PlayerFloatHelper.removeView(mContext) PlayerFloatHelper.removeView()
} }
val music = Music() val music = Music()
...@@ -310,10 +308,8 @@ class CourseItemNewView : ConstraintLayout { ...@@ -310,10 +308,8 @@ class CourseItemNewView : ConstraintLayout {
) )
checkPermission() checkPermission()
if (PlayerFloatHelper.isShow(mContext)) { if (PlayerFloatHelper.isShow()) {
PlayerFloatHelper.hide() PlayerFloatHelper.removeResetView()
PlayerFloatHelper.removeView(mContext)
AudioPlayer.get().resetPlayer()
} }
} }
...@@ -324,8 +320,7 @@ class CourseItemNewView : ConstraintLayout { ...@@ -324,8 +320,7 @@ class CourseItemNewView : ConstraintLayout {
} }
override fun onPlayFinish() { override fun onPlayFinish() {
PlayerFloatHelper.hide() PlayerFloatHelper.removeResetView()
AudioPlayer.get().resetPlayer()
} }
override fun onPauseClick() { override fun onPauseClick() {
......
...@@ -552,8 +552,8 @@ public class FMDetailActivity extends BaseActivity implements View.OnClickListen ...@@ -552,8 +552,8 @@ public class FMDetailActivity extends BaseActivity implements View.OnClickListen
super.onResume(); super.onResume();
fmSurfaceView.surfaceCreated(null); fmSurfaceView.surfaceCreated(null);
if (PlayerFloatHelper.Companion.isShow(this)) { if (PlayerFloatHelper.Companion.isShow()) {
PlayerFloatHelper.Companion.removeView(this); PlayerFloatHelper.Companion.removeView();
} }
} }
......
...@@ -507,9 +507,9 @@ open class HomeBaseImpl : IHomeBaseEvent { ...@@ -507,9 +507,9 @@ open class HomeBaseImpl : IHomeBaseEvent {
} }
PlayerFloatHelper.isCanClick = true PlayerFloatHelper.isCanClick = true
if (PlayerFloatHelper.isShow(mContext!!)) { if (PlayerFloatHelper.isShow()) {
if (PlayerFloatHelper.playingType != PlayTypeEnum.PLAY_TYPE_FM) { if (PlayerFloatHelper.playingType != PlayTypeEnum.PLAY_TYPE_FM) {
PlayerFloatHelper.removeView(mContext!!) PlayerFloatHelper.removeView()
PlayerFloatHelper.show(mContext!!) PlayerFloatHelper.show(mContext!!)
} else { } else {
......
...@@ -109,9 +109,7 @@ class PlayMeditationActivity : BaseActivity() { ...@@ -109,9 +109,7 @@ class PlayMeditationActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
PlayerFloatHelper.removeView(mContext) PlayerFloatHelper.removeResetView()
PlayerFloatHelper.playTempData.clear()
AudioPlayer.get().resetPlayer()
mMediaPlayer = MediaPlayerManager.getInstance(this)?.getMediaPlayer() mMediaPlayer = MediaPlayerManager.getInstance(this)?.getMediaPlayer()
......
...@@ -5,6 +5,7 @@ import android.content.IntentFilter ...@@ -5,6 +5,7 @@ import android.content.IntentFilter
import android.media.AudioManager import android.media.AudioManager
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.util.Log
import com.tencent.bugly.Bugly.applicationContext import com.tencent.bugly.Bugly.applicationContext
import com.ydl.media.audio.enums.PlayModeEnum import com.ydl.media.audio.enums.PlayModeEnum
import com.ydl.media.audio.manager.AudioFocusManager import com.ydl.media.audio.manager.AudioFocusManager
...@@ -166,13 +167,16 @@ class AudioPlayer private constructor() { ...@@ -166,13 +167,16 @@ class AudioPlayer private constructor() {
} }
when (playMode) { when (playMode) {
PlayModeEnum.SINGLE -> { // 这是老逻辑 PlayModeEnum.SINGLE -> {
resetPlayer() resetPlayer()
playMode = PlayModeEnum.LIST_LOOP playMode = PlayModeEnum.LIST_LOOP
} }
PlayModeEnum.LIST -> { PlayModeEnum.LIST -> {
if (playPosition >= musicList.lastIndex) pausePlayer() if (playPosition >= musicList.lastIndex) pausePlayer()
else next() else {
Log.d("wjy", "====OnCompletion==== $playPosition")
next()
}
} }
else -> next() else -> next()
} }
...@@ -395,6 +399,16 @@ class AudioPlayer private constructor() { ...@@ -395,6 +399,16 @@ class AudioPlayer private constructor() {
state = STATE_IDLE state = STATE_IDLE
} }
fun getNextPosition(position: Int): Int {
return when (playMode) {
PlayModeEnum.SHUFFLE -> Random().nextInt(musicList.size)
PlayModeEnum.SINGLE_LOOP -> position
PlayModeEnum.LIST_LOOP -> if (position >= musicList.lastIndex) 0 else position + 1
PlayModeEnum.LIST -> if (position >= musicList.lastIndex) -1 else position + 1
else -> position + 1
}
}
/** /**
* 下一首 * 下一首
*/ */
...@@ -402,17 +416,12 @@ class AudioPlayer private constructor() { ...@@ -402,17 +416,12 @@ class AudioPlayer private constructor() {
if (musicList.isEmpty()) { if (musicList.isEmpty()) {
return return
} }
val position = when (playMode) { val position = getNextPosition(playPosition)
PlayModeEnum.SHUFFLE -> Random().nextInt(musicList.size)
PlayModeEnum.SINGLE_LOOP -> playPosition
PlayModeEnum.LIST_LOOP -> if (playPosition >= musicList.lastIndex) 0 else playPosition + 1
PlayModeEnum.LIST -> if (playPosition >= musicList.lastIndex) -1 else playPosition + 1
else -> playPosition + 1
}
if (-1 == position) { if (-1 == position) {
ToastUtil.toastShort("暂无内容") ToastUtil.toastShort("暂无内容")
return return
} }
Log.d("wjy", "next playPosition: $playPosition position: $position")
load(position) load(position)
} }
...@@ -445,7 +454,7 @@ class AudioPlayer private constructor() { ...@@ -445,7 +454,7 @@ class AudioPlayer private constructor() {
fun seekTo(percent: Int = -1, position: Long = -1) { fun seekTo(percent: Int = -1, position: Long = -1) {
if (isPlaying || isPausing) { if (isPlaying || isPausing) {
val duration = getDuration() val duration = getDuration()
var currentPercent = 0 val currentPercent: Int
val currentPosition = if (position != -1L) { val currentPosition = if (position != -1L) {
val pos = when { val pos = when {
position > duration -> duration position > duration -> duration
......
package com.ydl.media.view package com.ydl.media.view
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.PixelFormat
import android.graphics.Point
import android.os.Bundle import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import androidx.core.view.ViewCompat
import com.alibaba.android.arouter.launcher.ARouter import com.alibaba.android.arouter.launcher.ARouter
import com.ydl.media.audio.AudioPlayer import com.ydl.media.audio.AudioPlayer
import com.yidianling.common.tools.RxImageTool
/** /**
* 音频播放悬浮窗 帮助类 * 音频播放悬浮窗 帮助类
...@@ -24,32 +15,19 @@ import com.yidianling.common.tools.RxImageTool ...@@ -24,32 +15,19 @@ import com.yidianling.common.tools.RxImageTool
class PlayerFloatHelper { class PlayerFloatHelper {
companion object { companion object {
@SuppressLint("StaticFieldLeak")
private var mPlayerFloatView: PlayerFloatView? = null private var mPlayerFloatView: PlayerFloatView? = null
//已添加悬浮窗页面全路径名
private var showingPageName: String? = null
private var wm: WindowManager? = null
var isCanClick = true var isCanClick = true
var playingType = PlayTypeEnum.PLAY_TYPE_NONE var playingType = PlayTypeEnum.PLAY_TYPE_NONE
var playTempData = hashMapOf<String,String>() var playTempData = hashMapOf<String,String>()
/** /**
* FM:
* fmId
* fmTitle
* fmAuthor
* fmImageUrl
*/
/**
* 如果有音频正在播放则显示 * 如果有音频正在播放则显示
*/ */
fun showIfPlaying(context: Context) { fun showIfPlaying(context: Context) {
if (AudioPlayer.get().isPlaying) { if (AudioPlayer.get().isPlaying) {
show(context) show(context)
mPlayerFloatView?.updatePlayState()
} else { } else {
hide() removeView()
} }
} }
...@@ -57,43 +35,15 @@ class PlayerFloatHelper { ...@@ -57,43 +35,15 @@ class PlayerFloatHelper {
* 显示悬浮控件 * 显示悬浮控件
*/ */
fun show(context: Context, playTypeEnum: PlayTypeEnum = PlayTypeEnum.PLAY_TYPE_NONE, playData:HashMap<String,String> = hashMapOf()) { fun show(context: Context, playTypeEnum: PlayTypeEnum = PlayTypeEnum.PLAY_TYPE_NONE, playData:HashMap<String,String> = hashMapOf()) {
if (playData.size > 0) {
playingType = playTypeEnum
if (playData.size>0){
this.playTempData.putAll(playData) this.playTempData.putAll(playData)
} }
if (mPlayerFloatView == null) { if (mPlayerFloatView == null) {
mPlayerFloatView = PlayerFloatView(context) playingType = playTypeEnum
mPlayerFloatView?.addFloatClickListener(object :PlayerFloatView.FloatViewPlayListener{ mPlayerFloatView = PlayerFloatView(context).show()
override fun onPauseClick() {
}
override fun onStartClick() {
}
override fun onPlayFinish() {
}
})
}
if (showingPageName != context::class.qualifiedName) {
mPlayerFloatView?.resetLayoutParams()
addFloatToWm(context)
} }
mPlayerFloatView?.updatePlayState()
mPlayerFloatView?.resetFloatView()
mPlayerFloatView?.visibility = View.VISIBLE
mPlayerFloatView?.setPlayingState()
}
fun hide() {
mPlayerFloatView?.visibility = View.GONE
} }
fun addClickListener(listener: PlayerFloatView.FloatViewPlayListener) { fun addClickListener(listener: PlayerFloatView.FloatViewPlayListener) {
...@@ -104,31 +54,32 @@ class PlayerFloatHelper { ...@@ -104,31 +54,32 @@ class PlayerFloatHelper {
mPlayerFloatView?.removeFloatClickListener(listener) mPlayerFloatView?.removeFloatClickListener(listener)
} }
fun isShow(context: Context): Boolean { fun isShow(): Boolean {
return !TextUtils.isEmpty(showingPageName) && showingPageName == context::class.qualifiedName && mPlayerFloatView?.visibility == View.VISIBLE return null != mPlayerFloatView
} }
fun removeView(context: Context) { /**
// if (TextUtils.isEmpty(showingPageName) || showingPageName != context::class.qualifiedName) { * 只移除悬浮窗
// return */
// } fun removeView() {
mPlayerFloatView?.visibility = View.GONE playTempData.clear()
wm?.removeViewImmediate(mPlayerFloatView) mPlayerFloatView?.let {
showingPageName = "" it.remove()
wm = null it.removeAllViews()
}
mPlayerFloatView = null
} }
fun onDestroy() { /**
if (mPlayerFloatView != null) { * 移除悬浮窗 + 重置音频播放器
if (!TextUtils.isEmpty(showingPageName)) { */
wm?.removeViewImmediate(mPlayerFloatView) fun removeResetView() {
showingPageName = "" playTempData.clear()
} mPlayerFloatView?.let {
it.removeAndReset()
mPlayerFloatView?.onDestroy() it.removeAllViews()
mPlayerFloatView?.removeAllViews()
} }
mPlayerFloatView = null
} }
fun setPlayingState(context: Context) { fun setPlayingState(context: Context) {
...@@ -140,46 +91,13 @@ class PlayerFloatHelper { ...@@ -140,46 +91,13 @@ class PlayerFloatHelper {
mPlayerFloatView?.updatePlayState() mPlayerFloatView?.updatePlayState()
} }
private fun addFloatToWm(context: Context) {
if (wm != null && !TextUtils.isEmpty(showingPageName)) {
if (ViewCompat.isAttachedToWindow(mPlayerFloatView!!)){
// if (context is Activity && !(context.isFinishing)){
// wm?.removeViewImmediate(mPlayerFloatView)
// }
wm?.removeViewImmediate(mPlayerFloatView)
}
wm = null
}
//获取WindowManager
wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
//设置LayoutParams(全局变量)相关参数
val wmParams = mPlayerFloatView?.mFloatViewParams
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) { fun startPlayingActivity(context: Context?, fullScreen: Int = 0) {
if (playingType == PlayTypeEnum.PLAY_TYPE_FM) { if (playingType == PlayTypeEnum.PLAY_TYPE_FM) {
//FM播放页 //FM播放页
startFMPlayActivity(context) startFMPlayActivity()
} else if (playingType == PlayTypeEnum.PLAY_TYPE_COURSE) { } else if (playingType == PlayTypeEnum.PLAY_TYPE_COURSE) {
val url = AudioPlayer.get().playMusic?.path val url = AudioPlayer.get().playMusic?.path
//课程播放页 //课程播放页
...@@ -212,7 +130,7 @@ class PlayerFloatHelper { ...@@ -212,7 +130,7 @@ class PlayerFloatHelper {
} }
} }
fun startFMPlayActivity(context: Context?) { private fun startFMPlayActivity() {
val bundle = Bundle() val bundle = Bundle()
bundle.putInt("id", playTempData["fmId"]?.toInt()?:0) bundle.putInt("id", playTempData["fmId"]?.toInt()?:0)
ARouter.getInstance().build("/fm/detail") ARouter.getInstance().build("/fm/detail")
...@@ -227,9 +145,6 @@ class PlayerFloatHelper { ...@@ -227,9 +145,6 @@ class PlayerFloatHelper {
return playTempData["fmId"]?.toInt()?:0 return playTempData["fmId"]?.toInt()?:0
} }
} }
} }
package com.ydl.media.view package com.ydl.media.view
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Application
import android.content.Context import android.content.Context
import android.graphics.PixelFormat
import android.graphics.Point
import android.os.Build
import android.text.TextUtils import android.text.TextUtils
import android.view.MotionEvent import android.util.Log
import android.view.View import android.view.*
import android.view.ViewConfiguration
import android.view.WindowManager
import android.widget.FrameLayout import android.widget.FrameLayout
import com.bumptech.glide.Glide
import com.ydl.media.R import com.ydl.media.R
import com.ydl.media.audio.AudioPlayer import com.ydl.media.audio.AudioPlayer
import com.ydl.media.audio.OnPlayerEventListener import com.ydl.media.audio.OnPlayerEventListener
import com.ydl.media.audio.model.Music import com.ydl.media.audio.model.Music
import com.ydl.ydl_image.module.GlideApp
import com.ydl.ydl_image.transform.GlideRoundTransform
import com.ydl.ydlcommon.utils.statusBar.StatusBarUtil import com.ydl.ydlcommon.utils.statusBar.StatusBarUtil
import com.yidianling.common.tools.RxImageTool
import kotlinx.android.synthetic.main.item_playing_float_btn.view.* import kotlinx.android.synthetic.main.item_playing_float_btn.view.*
import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.CopyOnWriteArraySet
import kotlin.math.abs import kotlin.math.abs
import com.ydl.ydl_image.transform.GlideRoundTransform
class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) { class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
...@@ -38,16 +39,11 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) { ...@@ -38,16 +39,11 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
private val mStatusBarHeight: Int = StatusBarUtil.getStatusBarHeight(context) private val mStatusBarHeight: Int = StatusBarUtil.getStatusBarHeight(context)
fun resetLayoutParams() {
mFloatViewParams = WindowManager.LayoutParams()
}
init { init {
View.inflate(context, R.layout.item_playing_float_btn, this) View.inflate(context, R.layout.item_playing_float_btn, this)
resetFloatView() resetFloatView()
if (!TextUtils.isEmpty(AudioPlayer.get().playMusic?.coverPath)) { if (!TextUtils.isEmpty(AudioPlayer.get().playMusic?.coverPath)) {
GlideApp.with(context) Glide.with(context)
.load(AudioPlayer.get().playMusic?.coverPath) .load(AudioPlayer.get().playMusic?.coverPath)
.transform(GlideRoundTransform(context, 4)) .transform(GlideRoundTransform(context, 4))
.error(R.drawable.ico_play_float_pic) .error(R.drawable.ico_play_float_pic)
...@@ -62,9 +58,7 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) { ...@@ -62,9 +58,7 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
mListeners.forEach { mListeners.forEach {
it.onPauseClick() it.onPauseClick()
} }
PlayerFloatHelper.removeView(context) PlayerFloatHelper.removeResetView()
PlayerFloatHelper.playTempData.clear()
AudioPlayer.get().resetPlayer()
} }
play_state.setOnClickListener { play_state.setOnClickListener {
if (AudioPlayer.get().isPlaying) { if (AudioPlayer.get().isPlaying) {
...@@ -84,7 +78,7 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) { ...@@ -84,7 +78,7 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
override fun onComplete() { override fun onComplete() {
tv_start.text = getStringTime(AudioPlayer.get().getDuration().toInt()) tv_start.text = getStringTime(AudioPlayer.get().getDuration().toInt())
play_state.setImageResource(R.drawable.ico_yyfc_play) setPlayDrawable()
mListeners.forEach { mListeners.forEach {
it.onPlayFinish() it.onPlayFinish()
} }
...@@ -92,12 +86,15 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) { ...@@ -92,12 +86,15 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
override fun onPreLoad(playPosition: Int): Boolean { override fun onPreLoad(playPosition: Int): Boolean {
AudioPlayer.get().getMusicList().elementAtOrNull(playPosition)?.let { AudioPlayer.get().getMusicList().elementAtOrNull(playPosition)?.let {
if (it.canPlay) { return if (it.canPlay) {
Log.d("wjy", "onPreLoad true $playPosition")
tv_title.text = it.title tv_title.text = it.title
tv_name.text = it.artist tv_name.text = it.artist
return true true
} else { } else {
AudioPlayer.get().next() Log.d("wjy", "onPreLoad false $playPosition")
AudioPlayer.get().load(AudioPlayer.get().getNextPosition(playPosition))
false
} }
} }
return true return true
...@@ -107,11 +104,11 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) { ...@@ -107,11 +104,11 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
} }
override fun onStartPlay() { override fun onStartPlay() {
play_state.setImageResource(R.drawable.ico_yyfc_pause) setPauseDrawable()
} }
override fun onPausePlay() { override fun onPausePlay() {
play_state.setImageResource(R.drawable.ico_yyfc_play) setPlayDrawable()
} }
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
...@@ -134,9 +131,9 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) { ...@@ -134,9 +131,9 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
fun resetFloatView() { private fun resetFloatView() {
if (!TextUtils.isEmpty(AudioPlayer.get().playMusic?.coverPath)) { if (!TextUtils.isEmpty(AudioPlayer.get().playMusic?.coverPath)) {
GlideApp.with(context) Glide.with(context)
.load(AudioPlayer.get().playMusic?.coverPath) .load(AudioPlayer.get().playMusic?.coverPath)
.transform(GlideRoundTransform(context, 4)) .transform(GlideRoundTransform(context, 4))
.error(R.drawable.ico_play_float_pic) .error(R.drawable.ico_play_float_pic)
...@@ -229,23 +226,58 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) { ...@@ -229,23 +226,58 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
fun updatePlayState() { fun updatePlayState() {
if (AudioPlayer.get().isPlaying) { if (AudioPlayer.get().isPlaying) {
play_state.setImageResource(R.drawable.ico_yyfc_pause) setPauseDrawable()
} else { } else {
play_state.setImageResource(R.drawable.ico_yyfc_play) setPlayDrawable()
for (listener in mListeners) { for (listener in mListeners) {
listener.onPauseClick() listener.onPauseClick()
} }
} }
} }
fun setPlayingState() { private fun setPlayDrawable() {
play_state.setImageResource(R.drawable.ico_yyfc_play)
}
private fun setPauseDrawable() {
play_state.setImageResource(R.drawable.ico_yyfc_pause) play_state.setImageResource(R.drawable.ico_yyfc_pause)
}
fun setPlayingState() {
setPauseDrawable()
resetFloatView() resetFloatView()
} }
fun onDestroy() { fun show(): PlayerFloatView {
mWindowManager.addView(this, mFloatViewParams.apply {
type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
WindowManager.LayoutParams.TYPE_PHONE;
}
format = PixelFormat.RGBA_8888 // 设置图片格式,效果为背景透明
flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
gravity = Gravity.START or Gravity.TOP // 调整悬浮窗口至左上角
val size = Point() // 以屏幕左上角为原点,设置x、y初始值
mWindowManager.defaultDisplay?.getSize(size)
x = 0
y = size.y * 5 / 6
//设置悬浮窗口长宽数据
width = WindowManager.LayoutParams.MATCH_PARENT
height = RxImageTool.dp2px(56f)
})
return this
}
fun remove() {
mListeners.clear() mListeners.clear()
mStateChangeListener?.let { AudioPlayer.get().removeOnPlayEventListener(it) } mStateChangeListener?.let { AudioPlayer.get().removeOnPlayEventListener(it) }
mWindowManager.removeViewImmediate(this)
}
fun removeAndReset() {
AudioPlayer.get().resetPlayer()
remove()
} }
fun addFloatClickListener(floatClickListener: FloatViewPlayListener) { fun addFloatClickListener(floatClickListener: FloatViewPlayListener) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment