Commit 2b429e33 by 王佳洋

悬浮窗点击X关闭悬浮窗和音频时,如果之前的播放页并未关闭,再次回到播放页,点击播放按钮无效

此bug线上就存在,在新写的音频播放页修复此bug,之前的老逻辑不做改动
parent cb0f4283
......@@ -14,6 +14,7 @@ import com.bumptech.glide.request.target.ViewTarget
import com.bumptech.glide.request.transition.Transition
import com.ydl.media.audio.model.Music
import com.ydl.media.view.PlayerFloatHelper
import com.ydl.media.view.PlayerFloatView
import com.ydl.webview.H5Params
import com.ydl.webview.NewH5Activity
import com.ydl.ydlcommon.base.BaseMvpActivity
......@@ -46,7 +47,7 @@ import java.util.*
*/
@Route(path = "/course/audioPlay")
class AudioPlayActivity : BaseMvpActivity<IAudioPlayContract.View, IAudioPlayContract.Presenter>(),
IAudioPlayContract.View {
IAudioPlayContract.View, PlayerFloatView.FloatViewPlayListener {
private var mCourseSpeedDialog: CourseSpeedDialog? = null
private var mCourseListDialog: CourseListDialog? = null
......@@ -234,11 +235,11 @@ class AudioPlayActivity : BaseMvpActivity<IAudioPlayContract.View, IAudioPlayCon
}
private fun speedClick() {
val audioPlay = audio_play
showFragment(mCourseSpeedDialog ?: CourseSpeedDialog().apply {
mCourseSpeedDialog = this
mListener = { speed ->
audioPlay.setSpeed(speed)
showFragment(mCourseSpeedDialog ?: CourseSpeedDialog().also { dialog ->
mCourseSpeedDialog = dialog
dialog.setSpeedPosition(audio_play.getSpeed())
dialog.mListener = { speed ->
audio_play.setSpeed(speed)
}
}, "courseSpeed")
}
......@@ -267,8 +268,20 @@ class AudioPlayActivity : BaseMvpActivity<IAudioPlayContract.View, IAudioPlayCon
}
}
override fun onStop() {
PlayerFloatHelper.addClickListener(this)
super.onStop()
}
override fun onDestroy() {
audio_play.onDestroy()
PlayerFloatHelper.removeClickListener(this)
super.onDestroy()
}
override fun onPreClose(): Boolean {
audio_play.pausePlay()
PlayerFloatHelper.removeView()
return false
}
}
\ No newline at end of file
......@@ -197,13 +197,14 @@ class AudioPlayView(context: Context, attrs: AttributeSet?) :
return "$m:$s"
}
fun onDestroy() {
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
AudioPlayer.get().removeOnPlayEventListener(this)
}
fun setSpeed(speed: Float) {
AudioPlayer.get().setSpeed(speed)
}
fun setSpeed(speed: Float) = AudioPlayer.get().setSpeed(speed)
fun getSpeed() = AudioPlayer.get().getSpeed()
fun isPlaying() = AudioPlayer.get().isPlaying
......
......@@ -18,8 +18,11 @@ import kotlinx.android.synthetic.main.item_course_speed.view.*
*/
class CourseSpeedDialog : BaseDialogFragment() {
private val mSpeedList = floatArrayOf(0.5f, 0.75f, 1.0f, 1.25f, 1.5f, 2.0f, 3.0f)
var mListener: ((speed: Float) -> Unit)? = null
private var mAdapter: CourseSpeedAdapter? = null
private var mOldPosition = -1
private var mCurrentPosition = 2
override fun getLayoutResourceId(): Int = R.layout.dialog_course_speed
override fun getGravity(): Int = Gravity.BOTTOM
......@@ -34,15 +37,15 @@ class CourseSpeedDialog : BaseDialogFragment() {
}
}
fun setSpeedPosition(speed: Float) {
mCurrentPosition = mSpeedList.indexOf(speed)
}
inner class CourseSpeedAdapter(
context: Context,
private val inflater: LayoutInflater = LayoutInflater.from(context)
) : RecyclerView.Adapter<CourseSpeedAdapter.CourseSpeedVH>() {
private val mSpeedList = floatArrayOf(0.5f, 0.75f, 1.0f, 1.25f, 1.5f, 2.0f, 3.0f)
private var mOldPosition = -1
private var mCurrentPosition = 2
inner class CourseSpeedVH(view: View) : RecyclerView.ViewHolder(view)
override fun onCreateViewHolder(viewGroup: ViewGroup, itemType: Int): CourseSpeedVH =
......
......@@ -516,9 +516,14 @@ class AudioPlayer private constructor() {
}
/**
* 倍速播放
* 设置倍速播放
*/
fun setSpeed(speed: Float) {
(mediaPlayer as? IjkMediaPlayer)?.setSpeed(speed)
}
/**
* 获取播放倍速
*/
fun getSpeed(): Float = (mediaPlayer as? IjkMediaPlayer)?.getSpeed(0f) ?: 1f
}
......@@ -34,8 +34,8 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
//全局变量,用以保存悬浮窗口的属性
var mFloatViewParams = WindowManager.LayoutParams()
private var mListeners: CopyOnWriteArraySet<FloatViewPlayListener> = CopyOnWriteArraySet()
private var mStateChangeListener: OnPlayerEventListener? = null
private var mFloatViewPlayListeners = HashSet<FloatViewPlayListener>()
private var mAudioPlayListener: OnPlayerEventListener? = null
private val mStatusBarHeight: Int = StatusBarUtil.getStatusBarHeight(context)
......@@ -55,7 +55,10 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
}
}
play_close.setOnClickListener {
mListeners.forEach {
mFloatViewPlayListeners.forEach {
if (!it.onPreClose()) return@setOnClickListener
}
mFloatViewPlayListeners.forEach {
it.onPauseClick()
}
PlayerFloatHelper.removeResetView()
......@@ -63,23 +66,23 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
play_state.setOnClickListener {
if (AudioPlayer.get().isPlaying) {
AudioPlayer.get().pausePlayer()
mListeners.forEach {
mFloatViewPlayListeners.forEach {
it.onPauseClick()
}
} else if (AudioPlayer.get().isPausing) {
AudioPlayer.get().startPlayer()
mListeners.forEach {
mFloatViewPlayListeners.forEach {
it.onStartClick()
}
}
}
if (mStateChangeListener == null) {
mStateChangeListener = object : OnPlayerEventListener {
if (mAudioPlayListener == null) {
mAudioPlayListener = object : OnPlayerEventListener {
override fun onComplete() {
tv_start.text = getStringTime(AudioPlayer.get().getDuration().toInt())
setPlayDrawable()
mListeners.forEach {
mFloatViewPlayListeners.forEach {
it.onPlayFinish()
}
}
......@@ -125,7 +128,7 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
updatePlayState()
}
}
AudioPlayer.get().addOnPlayEventListener(mStateChangeListener!!)
AudioPlayer.get().addOnPlayEventListener(mAudioPlayListener!!)
}
}
......@@ -229,7 +232,7 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
setPauseDrawable()
} else {
setPlayDrawable()
for (listener in mListeners) {
for (listener in mFloatViewPlayListeners) {
listener.onPauseClick()
}
}
......@@ -270,8 +273,8 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
}
fun remove() {
mListeners.clear()
mStateChangeListener?.let { AudioPlayer.get().removeOnPlayEventListener(it) }
mFloatViewPlayListeners.clear()
mAudioPlayListener?.let { AudioPlayer.get().removeOnPlayEventListener(it) }
mWindowManager.removeViewImmediate(this)
}
......@@ -281,20 +284,25 @@ class PlayerFloatView(ctx: Context) : FrameLayout(ctx.applicationContext) {
}
fun addFloatClickListener(floatClickListener: FloatViewPlayListener) {
mListeners.add(floatClickListener)
mFloatViewPlayListeners.add(floatClickListener)
}
fun removeFloatClickListener(floatClickListener: FloatViewPlayListener) {
mListeners.remove(floatClickListener)
mFloatViewPlayListeners.remove(floatClickListener)
}
interface FloatViewPlayListener {
fun onPauseClick()
{}
fun onStartClick()
{}
fun onPlayFinish()
{}
/**
* @return true: 允许关闭
*/
fun onPreClose(): Boolean = true
}
}
\ No newline at end of file
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