罗明文
2024-06-19 481723ce3c05d74fec53b8567b9c79d77bdcc155
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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package com.dollearn.student.ui.home
 
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import androidx.core.os.bundleOf
import cn.sinata.xldutils.fragment.BaseFragment
import cn.sinata.xldutils.gone
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.fragmetn_look_img.*
 
class LookImgFragment:BaseFragment(), AudioUtils.OnAudioStatusUpdateListener {
    override fun contentViewId() = R.layout.fragmetn_look_img
 
    private val TAG = "Choose=======》"
 
    private var handler:Handler? = null
 
    private val index by lazy { arguments?.getInt("index")?:0 }
    private val data by lazy {
        (requireActivity() as LookImgActivity).data
    }
    private val act by lazy { (requireActivity() as LookImgActivity) }
    private val voiceList = arrayListOf<String>()
 
    private val PLAY_VOICE = 1
    private val TO_NEXT = 10
    private val player by lazy { AudioUtils() }
 
    private var voiceIndex = -1 //点击播放的声音序号 0-2取值
    private var playing = false //播放中,不能有任何操作
 
    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 (voiceIndex<voiceList.size)
                            player.startPlayMusic(requireContext(),voiceList[voiceIndex])
                    }
                    TO_NEXT->{
                        if (isAdded&&!playing)
                            (requireActivity() as LookImgActivity).next()
                    }
                }
            }
        }
        data?.storyList?.get(index)?.apply {
            iv_1.setImageURI(img)
            voiceList.add(correct)
            voiceList.addAll(error!!.split(","))
            voiceList.shuffle()
        }
        recover()
        setOnClick()
    }
 
    private fun setOnClick() {
        cl_voice1.clickDelay {
            if (playing)
                return@clickDelay
            voiceIndex = 0
            handler?.sendEmptyMessage(PLAY_VOICE)
        }
 
        cl_voice2.clickDelay {
            if (playing)
                return@clickDelay
            voiceIndex = 1
            handler?.sendEmptyMessage(PLAY_VOICE)
        }
 
        cl_voice3.clickDelay {
            if (playing)
                return@clickDelay
            voiceIndex = 2
            handler?.sendEmptyMessage(PLAY_VOICE)
        }
 
        rl_check_1.clickDelay {
            if (playing)
                return@clickDelay
            dot_1.visible()
            act.totalCount++
            if (voiceList[0] == data!!.storyList[index].correct){
                act.rightCount++
                data!!.storyList[index].completed = true
                motion.transitionToEnd()
                cl_voice1.callOnClick()
            }else{
                iv_error_1.visible()
                dot_1.postDelayed({
                    iv_error_1.gone()
                    dot_1.gone()
                },2000)
            }
        }
 
        rl_check_2.clickDelay {
            if (playing)
                return@clickDelay
            dot_2.visible()
            act.totalCount++
            if (voiceList[1] == data!!.storyList[index].correct){
                act.rightCount++
                data!!.storyList[index].completed = true
                motion.transitionToEnd()
                cl_voice2.callOnClick()
            }else{
                iv_error_2.visible()
                dot_1.postDelayed({
                    dot_2.gone()
                    iv_error_2.gone()
                },2000)
            }
        }
        rl_check_3.clickDelay {
            if (playing)
                return@clickDelay
            dot_3.visible()
            act.totalCount++
            if (voiceList[2] == data!!.storyList[index].correct){
                data!!.storyList[index].completed = true
                act.rightCount++
                motion.transitionToEnd()
                cl_voice3.callOnClick()
            }else{
                iv_error_3.visible()
                dot_1.postDelayed({
                    iv_error_3.gone()
                    dot_3.gone()
                },2000)
            }
        }
    }
 
    /**
     * 重置
     */
    fun recover() {
        voiceList.shuffle()
        rl_check_1.gone()
        rl_check_2.gone()
        rl_check_3.gone()
        dot_1.gone()
        dot_2.gone()
        dot_3.gone()
        //设置动画飞行路径 3对1
        val rightVoiceIndex = voiceList.indexOf(data!!.storyList[index].correct)
        var start = 0
        var end = 0
        when(rightVoiceIndex){
            0->{
                start = R.id.start_1
                end = R.id.end_1
            }
            1->{
                start = R.id.start_2
                end = R.id.end_2
            }
            2->{
                start = R.id.start_3
                end = R.id.end_3
            }
        }
        motion.progress = 0f
        motion.setTransition(start,end)
        data!!.storyList[index].completed = false
    }
 
    companion object{
        /**
         * @param index 题组号
         */
        fun getInstance(index:Int):LookImgFragment{
            val listenFragment = LookImgFragment()
            listenFragment.arguments = bundleOf("index" to index)
            return listenFragment
        }
    }
 
    override fun onUpdate(db: Double, time: Long) {
    }
 
    override fun onStop(filePath: String?) {
    }
 
    override fun onStartPlay() {
        playing = true
        if (voiceIndex == 0){
            iv1_1.gone()
            iv2_1.gone()
            iv_playing_1.visible()
        }
        if (voiceIndex == 1){
            iv1_2.gone()
            iv2_2.gone()
            iv_playing_2.visible()
        }
        if (voiceIndex == 2){
            iv1_3.gone()
            iv2_3.gone()
            iv_playing_3.visible()
        }
    }
 
    override fun onFinishPlay() {
        playing = false
        if (voiceIndex == 0){
            rl_check_1.visible()
            iv1_1.visible()
            iv2_1.visible()
            iv_playing_1.gone()
        }
        if (voiceIndex == 1){
            rl_check_2.visible()
            iv1_2.visible()
            iv2_2.visible()
            iv_playing_2.gone()
        }
        if (voiceIndex == 2){
            rl_check_3.visible()
            iv1_3.visible()
            iv2_3.visible()
            iv_playing_3.gone()
        }
        if (data!!.storyList[index].completed){ //本题已答对
            Log.e(TAG,"选对答案后的播放结束3秒后进入下一图")
            handler?.sendEmptyMessageDelayed(TO_NEXT,3000)
        }
    }
 
}