From c303346ae803dc2a89ec0f025192773211861915 Mon Sep 17 00:00:00 2001 From: lmw <125975490@qq.com> Date: 星期二, 16 七月 2024 14:46:28 +0800 Subject: [PATCH] fix bug --- app/src/main/java/com/dollearn/student/ui/home/SuperListenActivity.kt | 121 ++++++++++++++++++++++++++-------------- 1 files changed, 79 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/dollearn/student/ui/home/SuperListenActivity.kt b/app/src/main/java/com/dollearn/student/ui/home/SuperListenActivity.kt index e3332e5..a84aa71 100644 --- a/app/src/main/java/com/dollearn/student/ui/home/SuperListenActivity.kt +++ b/app/src/main/java/com/dollearn/student/ui/home/SuperListenActivity.kt @@ -7,6 +7,7 @@ import android.view.View import androidx.recyclerview.widget.GridLayoutManager 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 @@ -19,13 +20,15 @@ import com.dollearn.student.ui.TransparentStatusBarActivity import com.dollearn.student.ui.home.adapter.GameAdapter import com.dollearn.student.utils.AudioUtils +import com.dollearn.student.utils.Const +import com.dollearn.student.utils.event.EmptyEvent import com.dollearn.student.utils.interfaces.StringCallback import kotlinx.android.synthetic.main.activity_super_listen.* import kotlinx.android.synthetic.main.fragmetn_look_img.* +import org.greenrobot.eventbus.EventBus class SuperListenActivity : TransparentStatusBarActivity(), AudioUtils.OnAudioStatusUpdateListener { override fun setContentView() = R.layout.activity_super_listen - private val week by lazy { intent.getIntExtra("week",0) @@ -33,64 +36,51 @@ private val season by lazy { intent.getIntExtra("season",0) } - private var data:GameBean? = null - private var index = 0 - private var handler:Handler? = null + var data:GameBean? = null + var index = 0 + var handler:Handler? = null private var difficulty = 0 //难度 - private val TAG = "Listen====>" + val TAG = "Listen====>" - private val list = arrayListOf<Subject>() + val list = arrayListOf<Subject>() private val adapter = GameAdapter(list) - private val PLAY_VOICE = 1 - private val COUNT_DOWN = 2 + val PLAY_VOICE = 1 + val COUNT_DOWN = 2 private val STUDY_TIME = 3 + + val PLAY_RIGHT = 4 + val PLAY_ERROR = 5 + + 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 val voiceList = arrayListOf<String>() //声音按顺序播放 + val voiceList = arrayListOf<String>() //声音按顺序播放 private var TIME = 10 //每题答题时间 - private var countTime = 10 //倒计时计数 + private var countTime = 0 //倒计时计数 - var totalCount = 0 //总答题次数 var rightCount = 0 //正确答题次数 var time = 0 //学习秒数 + var playing = false + + var state = 0 //0:准备听题 1:听题中 2:请作答 3:结束答题 + override fun initClick() { - adapter.setOnItemClickListener { view, position -> - Log.e(TAG,"点击图片:${list[position].name}") - Log.e(TAG,"音频position:${index}") - Log.e(TAG,"音频路径:${voiceList[index]}") - - val subject = data!!.subjectList[data!!.subjectList.map { it.correct }.indexOf(voiceList[index])] //当前音频对应题目 - Log.e(TAG,"正确答案:${subject.name}") - - totalCount++ - - handler?.removeMessages(COUNT_DOWN) //选择答案后,停止倒计时 - if (list[position].id == subject.id){ - rightCount++ - subject.completed = true - subject.right = true - handler?.sendEmptyMessage(PLAY_VOICE) - adapter.notifyItemChanged(position) - }else{ - subject.completed = true - subject.right = false - index++ - startGame() - } - } - tv_exit.setOnClickListener { if (tv_exit.text == "提交"){ - ResultActivity.startResult(this,0,0,0,6,totalCount,rightCount,list.filter { it.right }.sumBy { 1 },time,data!!.data.id) + ResultActivity.startResult(this,0,0,0,6,list.size,rightCount,list.filter { it.right }.sumBy { 1 },time,data!!.data.id,difficulty) finish() }else - finish() + onBackPressed() } } @@ -129,7 +119,10 @@ player.startPlayMusic(this@SuperListenActivity,voiceList[index]) } COUNT_DOWN->{ - countTime -- + if (!playing){ + countTime -- + state = 2 + } tv_tip.text = "请在${countTime}s内选择答案!" if ( countTime == 0){ index++ @@ -138,12 +131,25 @@ sendEmptyMessageDelayed(COUNT_DOWN,1000) } } + PLAY_RIGHT->{ + rightPlaying = true + player.startPlayMusic(this@SuperListenActivity,rightVoice) + } + PLAY_ERROR->{ + errorPlaying = true + player.startPlayMusic(this@SuperListenActivity,errorVoice) + } } } } + handler?.sendEmptyMessageDelayed(STUDY_TIME,1000) } private fun refreshUi() { + cl_voice.visible() + tv_sort.visible() + tv_exit.visible() + tv_tip.text = "准备听题" rv_list.layoutManager = GridLayoutManager(this,5) list.clear() list.addAll(data?.subjectList?: arrayListOf()) @@ -153,7 +159,7 @@ list.shuffle() //图片随机顺序 rv_list.adapter = adapter Log.e(TAG,"图片打乱后顺序:${list.joinToString(",,,") { it.name }}") - + TIME = data!!.data.time.toInt() val countdownDialog = CountdownDialog() countdownDialog.callback = object :StringCallback{ override fun onResult(rst: String) { @@ -164,17 +170,19 @@ countdownDialog.show(supportFragmentManager,"timer") } - private fun startGame() { + fun startGame() { if (index < voiceList.size){ Log.e(TAG,"开始答题:index=${index}") tv_sort.text = (index+1).toString() handler?.removeMessages(COUNT_DOWN) countTime = TIME //重置答题时间 tv_tip.text = "准备听题" - handler?.sendEmptyMessageDelayed(PLAY_VOICE,3000) + state = 0 + handler?.sendEmptyMessageDelayed(PLAY_VOICE,if (index == 0) 200 else 3000) }else{ tv_sort.visibility = View.INVISIBLE tv_tip.text = "" + state = 3 tv_end.text = "已完成全部问题" tv_exit.text = "提交" handler?.removeMessages(STUDY_TIME) @@ -188,7 +196,11 @@ } override fun onStartPlay() { + playing = true + if (errorPlaying||rightPlaying) + return if (!data!!.subjectList[data!!.subjectList.map { it.correct }.indexOf(voiceList[index])].completed){//首次播放 + state = 1 tv_tip.text = "请在${countTime}s内选择答案!" handler?.sendEmptyMessageDelayed(COUNT_DOWN,1000) } @@ -198,6 +210,7 @@ } override fun onFinishPlay() { + playing = false iv1.visible() iv2.visible() iv_playing.gone() @@ -205,6 +218,30 @@ index++ startGame() } + if (errorPlaying) + errorPlaying = false + if (rightPlaying){ + rightPlaying = false + val subject = data!!.subjectList[data!!.subjectList.map { it.correct }.indexOf(voiceList[index])] //当前音频对应题目 + subject.completed = true + handler?.sendEmptyMessage(PLAY_VOICE) + } } + override fun onBackPressed() { + HttpManager.exitGameOrStory(time).request(this){_,_-> + super.onBackPressed() + } + } + + override fun onDestroy() { + super.onDestroy() + handler?.removeCallbacksAndMessages(null) + EventBus.getDefault().post(EmptyEvent(Const.EventCode.STOP_TIMER)) + } + + override fun onPause() { + super.onPause() + player.stopPlayMusic() + } } -- Gitblit v1.7.1