lmw
2024-06-16 03172fc2d9a7717f4a9d8de1c5eca3158a550b30
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
package com.dollearn.student.ui.home
 
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import android.view.View
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.constraintlayout.motion.widget.MotionScene
import androidx.core.os.bundleOf
import cn.sinata.xldutils.fragment.BaseFragment
import cn.sinata.xldutils.gone
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.extention.clickDelay
import kotlinx.android.synthetic.main.fragment_listen.*
import org.jetbrains.anko.imageResource
 
class ListenFragment : BaseFragment(), AudioUtils.OnAudioStatusUpdateListener {
    override fun contentViewId() = R.layout.fragment_listen
 
    private val TAG = "Listen=======》"
 
    private var handler:Handler? = null
    private val PLAY_VOICE = 1
    private val TO_NEXT = 2
 
    private var correctVoice = "" //本题音频
    private val player by lazy { AudioUtils() }
    private val group by lazy { arguments?.getInt("group")?:0 }
    private val index by lazy { arguments?.getInt("index")?:0 }
    private val data by lazy {
        (requireActivity() as ListenActivity).data
    }
    private var hasListen = true //要听一次才能作答 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) }
 
    override fun onFirstVisibleToUser() {
        player.setOnAudioStatusUpdateListener(this)
        player.stopPlayMusic()
        handler = object : Handler(Looper.getMainLooper()){
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                when(msg.what){
                    PLAY_VOICE->{
                        if (iv_playing.visibility != View.VISIBLE)
                            player.startPlayMusic(requireContext(),correctVoice)
                    }
                    TO_NEXT->{
                        (requireActivity() as ListenActivity).next()
                    }
                }
            }
        }
        data?.apply {
            val originList = subjectList[group]
            correctVoice = originList[index].correct
            iv_1.setImageURI(randomList[0].img)
            iv_2.setImageURI(randomList[1].img)
            iv_3.setImageURI(randomList[2].img)
            iv_4.setImageURI(randomList[3].img)
            resultViewList.forEachIndexed { index, imageView ->
                imageView.imageResource = if (randomList[index].correct == correctVoice){
                    motion.setTransition(R.id.start,if (index == 0) R.id.end_1 else if (index == 1) R.id.end_2 else if (index == 2) R.id.end_3 else R.id.end_4)
                    R.mipmap.successs
                } else R.mipmap.zhifushibai
            }
        }
 
        handler?.sendEmptyMessageDelayed(PLAY_VOICE,1500) //进入题目2秒后播放,这里写1.5秒 预算进网络语音加载时间
 
        initClick()
    }
 
    private fun initClick() {
        cl_voice.clickDelay {
            handler?.sendEmptyMessage(PLAY_VOICE)
        }
        setResultAnimation(cl_1,0)
        setResultAnimation(cl_2,1)
        setResultAnimation(cl_3,2)
        setResultAnimation(cl_4,3)
    }
 
    /**
     * 回到上一题,恢复状态
     */
    fun recover(){
        Log.e(TAG,"回到上一题,恢复答题前的状态")
        hasListen = false
        hasRight = false
        handler?.sendEmptyMessageDelayed(PLAY_VOICE,1500) //进入题目2秒后播放,这里写1.5秒 预算进网络语音加载时间
        cl_1.progress = 0f
        cl_2.progress = 0f
        cl_3.progress = 0f
        cl_4.progress = 0f
        motion.progress = 0f
    }
 
    companion object{
        /**
         * @param group 题组号
         * @param index 组内序号
         */
        fun getInstance(group:Int,index:Int):ListenFragment{
            val listenFragment = ListenFragment()
            listenFragment.arguments = bundleOf("group" to group,"index" to index)
            return listenFragment
        }
    }
 
    override fun onUpdate(db: Double, time: Long) {
    }
 
    override fun onStop(filePath: String?) {
    }
 
    private fun setResultAnimation(v:MotionLayout,index: Int){
        v.clickDelay {
            if (!hasListen) {
                myToast("请先听题")
                return@clickDelay
            }
            if (!hasRight){
                v.progress = 0f
                v.transitionToEnd()
                if (randomList[index].correct != correctVoice){ //答案不对,结束后消失
                    v.setTransitionListener(object :MotionLayout.TransitionListener{
                        override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
                        }
 
                        override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, p3: Float) {
                        }
 
                        override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
                            v.progress = 0f
                        }
 
                        override fun onTransitionTrigger(p0: MotionLayout?, p1: Int, p2: Boolean, p3: Float) {
                        }
                    })
                }else{
                    hasRight = true
                    motion.transitionToEnd()
                    handler?.sendEmptyMessage(PLAY_VOICE)
                }
            }
        }
 
 
    }
 
    override fun onStartPlay() {
        iv1.gone()
        iv2.gone()
        iv_playing.visible()
    }
 
    override fun onFinishPlay() {
        iv1.visible()
        iv2.visible()
        iv_playing.gone()
        hasListen = true
        if (hasRight){ //选对答案后的播放结束3秒后进入下一图
            Log.e(TAG,"选对答案后的播放结束3秒后进入下一图")
            handler?.sendEmptyMessageDelayed(TO_NEXT,3000)
        }
    }
}