From b13afc751dbbce24753d008f1f87d2c5e133a4ad Mon Sep 17 00:00:00 2001
From: lmw <125975490@qq.com>
Date: 星期二, 09 七月 2024 15:19:26 +0800
Subject: [PATCH] fix bug

---
 app/src/main/java/com/dollearn/student/ui/home/LookImgFragment.kt |  145 ++++++++++++++++++++++++++++++++++--------------
 1 files changed, 102 insertions(+), 43 deletions(-)

diff --git a/app/src/main/java/com/dollearn/student/ui/home/LookImgFragment.kt b/app/src/main/java/com/dollearn/student/ui/home/LookImgFragment.kt
index 465177f..c90489b 100644
--- a/app/src/main/java/com/dollearn/student/ui/home/LookImgFragment.kt
+++ b/app/src/main/java/com/dollearn/student/ui/home/LookImgFragment.kt
@@ -4,12 +4,16 @@
 import android.os.Looper
 import android.os.Message
 import android.util.Log
+import android.view.View
 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.fragmetn_look_img.*
 
@@ -29,10 +33,22 @@
 
     private val PLAY_VOICE = 1
     private val TO_NEXT = 10
+
+    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 val player by lazy { AudioUtils() }
 
     private var voiceIndex = -1 //点击播放的声音序号 0-2取值
     private var playing = false //播放中,不能有任何操作
+
+    private var nowVoiceView: View? = null
 
     override fun onFirstVisibleToUser() {
         player.setOnAudioStatusUpdateListener(this)
@@ -42,12 +58,24 @@
                 super.handleMessage(msg)
                 when(msg.what){
                     PLAY_VOICE->{
+                        playing = true
                         if (voiceIndex<voiceList.size)
                             player.startPlayMusic(requireContext(),voiceList[voiceIndex])
                     }
                     TO_NEXT->{
                         if (isAdded&&!playing)
                             (requireActivity() as LookImgActivity).next()
+                    }
+                    PLAY_RIGHT->{
+                        rightPlaying = true
+                        player.startPlayMusic(requireContext(),rightVoice)
+                    }
+                    PLAY_ERROR->{
+                        rl_check_1.gone()
+                        rl_check_2.gone()
+                        rl_check_3.gone()
+                        errorPlaying = true
+                        player.startPlayMusic(requireContext(),errorVoice)
                     }
                 }
             }
@@ -87,57 +115,74 @@
         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()
+            if (rl_check_1.visibility == View.VISIBLE&&rl_check_2.visibility == View.VISIBLE&&rl_check_3.visibility == View.VISIBLE){
+                dot_1.visible()
+                act.totalCount++
+                if (voiceList[0] == data!!.storyList[index].correct){
+                    act.rightCount++
+                    motion.transitionToEnd()
+                    nowVoiceView = cl_voice1
+                    handler?.sendEmptyMessage(PLAY_RIGHT)
+
+                }else{
+                    handler?.sendEmptyMessage(PLAY_ERROR)
+                    iv_error_1.visible()
+                    dot_1.postDelayed({
+                        iv_error_1.gone()
+                        dot_1.gone()
+                    },2000)
+                }
             }else{
-                iv_error_1.visible()
-                dot_1.postDelayed({
-                    iv_error_1.gone()
-                    dot_1.gone()
-                },2000)
+                myToast("请听完")
             }
+
         }
 
         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)
-            }
+            if (rl_check_1.visibility == View.VISIBLE&&rl_check_2.visibility == View.VISIBLE&&rl_check_3.visibility == View.VISIBLE) {
+                dot_2.visible()
+                act.totalCount++
+                if (voiceList[1] == data!!.storyList[index].correct){
+                    act.rightCount++
+                    motion.transitionToEnd()
+                    nowVoiceView = cl_voice2
+                    handler?.sendEmptyMessage(PLAY_RIGHT)
+
+                }else{
+                    handler?.sendEmptyMessage(PLAY_ERROR)
+                    iv_error_2.visible()
+                    dot_1.postDelayed({
+                        dot_2.gone()
+                        iv_error_2.gone()
+                    },2000)
+                }
+            }else
+                myToast("请听完")
         }
         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)
-            }
+            if (rl_check_1.visibility == View.VISIBLE&&rl_check_2.visibility == View.VISIBLE&&rl_check_3.visibility == View.VISIBLE) {
+                dot_3.visible()
+                act.totalCount++
+                if (voiceList[2] == data!!.storyList[index].correct){
+                    act.rightCount++
+                    motion.transitionToEnd()
+                    nowVoiceView = cl_voice3
+                    handler?.sendEmptyMessage(PLAY_RIGHT)
+
+                }else{
+                    handler?.sendEmptyMessage(PLAY_ERROR)
+                    iv_error_3.visible()
+                    dot_1.postDelayed({
+                        iv_error_3.gone()
+                        dot_3.gone()
+                    },2000)
+                }
+            }else
+                myToast("请听完")
         }
     }
 
@@ -194,6 +239,8 @@
 
     override fun onStartPlay() {
         playing = true
+        if (rightPlaying||errorPlaying)
+            return
         if (voiceIndex == 0){
             iv1_1.gone()
             iv2_1.gone()
@@ -213,23 +260,31 @@
 
     override fun onFinishPlay() {
         playing = false
-        if (voiceIndex == 0){
+        if (voiceIndex == 0&&!errorPlaying){
             rl_check_1.visible()
             iv1_1.visible()
             iv2_1.visible()
             iv_playing_1.gone()
         }
-        if (voiceIndex == 1){
+        if (voiceIndex == 1&&!errorPlaying){
             rl_check_2.visible()
             iv1_2.visible()
             iv2_2.visible()
             iv_playing_2.gone()
         }
-        if (voiceIndex == 2){
+        if (voiceIndex == 2&&!errorPlaying){
             rl_check_3.visible()
             iv1_3.visible()
             iv2_3.visible()
             iv_playing_3.gone()
+        }
+        if (errorPlaying){
+            errorPlaying = false
+        }
+        if (rightPlaying){
+            rightPlaying = false
+            nowVoiceView?.callOnClick()
+            data!!.storyList[index].completed = true
         }
         if (data!!.storyList[index].completed){ //本题已答对
             Log.e(TAG,"选对答案后的播放结束3秒后进入下一图")
@@ -237,4 +292,8 @@
         }
     }
 
+    override fun onPause() {
+        super.onPause()
+        player.stopPlayMusic()
+    }
 }
\ No newline at end of file

--
Gitblit v1.7.1