lmw
1 天以前 9904e5f900ba751c1fe719cdf889f00e9f1418e8
app/src/main/java/com/dollearn/student/ui/home/ListenFragment.kt
@@ -10,10 +10,12 @@
import androidx.core.os.bundleOf
import cn.sinata.xldutils.fragment.BaseFragment
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.utils.SPUtils
import cn.sinata.xldutils.utils.myToast
import cn.sinata.xldutils.visible
import com.dollearn.student.R
import com.dollearn.student.utils.AudioUtils
import com.dollearn.student.utils.Const
import com.dollearn.student.utils.extention.clickDelay
import kotlinx.android.synthetic.main.fragment_listen.*
import org.jetbrains.anko.imageResource
@@ -26,6 +28,16 @@
    private var handler:Handler? = null
    private val PLAY_VOICE = 1
    private val TO_NEXT = 2
    private val PLAY_ERROR = 3
    private val PLAY_RIGHT = 4
    private val errorVoice by lazy { SPUtils.instance().getString(Const.EV) }
    private val rightVoice by lazy { SPUtils.instance().getString(Const.RV) }
    private var rightPlaying = false
    private var errorPlaying = false
    private var playing = false //播放中,不能有任何操作
    private var correctVoice = "" //本题音频
    private val player by lazy { AudioUtils() }
@@ -34,12 +46,14 @@
    private val data by lazy {
        (requireActivity() as ListenActivity).data
    }
    private var hasListen = true //要听一次才能作答 true:已经听完题目
    private var hasListen = false //要听一次才能作答 true:已经听完题目
    private var hasRight = false //正确以后不再做出选择 true:已经选出正确答案
    private val randomList by lazy { data?.subjectList!![group].shuffled() }//随机排序后的题目
    private val resultViewList by lazy { arrayListOf(result_1,result_2,result_3,result_4) }
    var right = true //true进入下一题为全对,false 一旦答错过就置为false,此时进入下一题,本题算做错误
    override fun onFirstVisibleToUser() {
        player.setOnAudioStatusUpdateListener(this)
@@ -49,6 +63,10 @@
                super.handleMessage(msg)
                when(msg.what){
                    PLAY_VOICE->{
                        playing = true
                        if (activity == null){
                            return
                        }
                        val listenActivity = activity as ListenActivity
                        val indexOf = listenActivity.fragments.indexOf(this@ListenFragment) //当前题目序号
                        if (iv_playing.visibility != View.VISIBLE&&indexOf == listenActivity.viewPager.currentItem){ //没有播放并且是当前题号
@@ -57,8 +75,20 @@
                        }
                    }
                    TO_NEXT->{
                        if (isAdded)
                        if (isAdded&&!playing){
                            (requireActivity() as ListenActivity).next()
                            removeMessages(TO_NEXT)
                        }
                    }
                    PLAY_RIGHT->{
                        playing = true
                        rightPlaying = true
                        player.startPlayMusic(requireContext(),rightVoice)
                    }
                    PLAY_ERROR->{
                        playing = true
                        errorPlaying = true
                        player.startPlayMusic(requireContext(),errorVoice)
                    }
                }
            }
@@ -78,14 +108,15 @@
            }
        }
        handler?.sendEmptyMessageDelayed(PLAY_VOICE,1500) //进入题目2秒后播放,这里写1.5秒 预算进网络语音加载时间
        handler?.sendEmptyMessageDelayed(PLAY_VOICE,200) //2025.4.8 立即播放
        initClick()
    }
    private fun initClick() {
        cl_voice.clickDelay {
            handler?.sendEmptyMessage(PLAY_VOICE)
            if (!playing)
                handler?.sendEmptyMessage(PLAY_VOICE)
        }
        setResultAnimation(cl_1,0)
        setResultAnimation(cl_2,1)
@@ -100,7 +131,8 @@
        Log.e(TAG,"回到上一题,恢复答题前的状态")
        hasListen = false
        hasRight = false
        handler?.sendEmptyMessageDelayed(PLAY_VOICE,1500) //进入题目2秒后播放,这里写1.5秒 预算进网络语音加载时间
        right = true
        handler?.sendEmptyMessageDelayed(PLAY_VOICE,200) //改为立即播放
        cl_1.progress = 0f
        cl_2.progress = 0f
        cl_3.progress = 0f
@@ -129,14 +161,19 @@
    private fun setResultAnimation(v:MotionLayout,index: Int){
        v.clickDelay {
            if (!hasListen) {
                myToast("请先听题")
//                myToast("请先听题")
                return@clickDelay
            }
            if (playing)
                    return@clickDelay
            (activity as ListenActivity).totalCount ++
            if (!hasRight){
                v.progress = 0f
                v.transitionToEnd()
                if (randomList[index].correct != correctVoice){ //答案不对,结束后消失
                    hasListen = false
                    right = false
                    handler?.sendEmptyMessage(PLAY_ERROR)
                    v.setTransitionListener(object :MotionLayout.TransitionListener{
                        override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
                        }
@@ -152,10 +189,9 @@
                        }
                    })
                }else{
                    hasRight = true
                    (activity as ListenActivity).rightCount ++
                    motion.transitionToEnd()
                    handler?.sendEmptyMessage(PLAY_VOICE)
                    handler?.sendEmptyMessage(PLAY_RIGHT)
                }
            }
        }
@@ -163,19 +199,41 @@
    }
    override fun onStartPlay() {
        iv1.gone()
        iv2.gone()
        iv_playing.visible()
        if (!errorPlaying&&!rightPlaying){
            iv1.gone()
            iv2.gone()
            iv_playing.visible()
        }
    }
    override fun onFinishPlay() {
        playing = false
        iv1.visible()
        iv2.visible()
        iv_playing.gone()
        hasListen = true
        if (hasRight){ //选对答案后的播放结束3秒后进入下一图
        if (hasRight&&!playing){ //选对答案后的播放结束3秒后进入下一图
            Log.e(TAG,"选对答案后的播放结束3秒后进入下一图")
            handler?.sendEmptyMessageDelayed(TO_NEXT,3000)
            handler?.sendEmptyMessageDelayed(TO_NEXT,500) //万俊杰:缩短为0.5秒
        }
        if (rightPlaying){
            rightPlaying = false
            hasRight = true
            handler?.sendEmptyMessage(PLAY_VOICE)
        }else if (errorPlaying){
            errorPlaying = false
            cl_voice.callOnClick()
        }
        else if (!hasRight)
            hasListen = true
    }
    override fun onPause() {
        super.onPause()
        player.stopPlayMusic()
    }
    override fun onDestroy() {
        super.onDestroy()
        handler?.removeCallbacksAndMessages(null)
    }
}