| | |
| | | import com.dollearn.student.utils.Const |
| | | import com.dollearn.student.utils.extention.clickDelay |
| | | import kotlinx.android.synthetic.main.fragment_include.* |
| | | import org.jetbrains.anko.support.v4.dip |
| | | import org.jetbrains.anko.support.v4.startActivity |
| | | |
| | | class IncludeFragment : BaseFragment(), AudioUtils.OnAudioStatusUpdateListener { |
| | |
| | | private val act by lazy { requireActivity() as IncludeActivity } |
| | | |
| | | private var voiceIndex = -1 //点击播放的声音序号 0-5取值 |
| | | private val voiceViews by lazy { arrayListOf(cl_voice1,cl_voice2,cl_voice3,cl_voice4,cl_voice5,cl_voice6) } |
| | | private val voiceViews by lazy { arrayListOf(cl_voice1_real,cl_voice2_real,cl_voice3_real,cl_voice4_real,cl_voice5_real,cl_voice6_real) } |
| | | private var playing = false |
| | | |
| | | private val answerImg = arrayListOf<String>() //随机答案图片 |
| | | private var currentImage = 0 //当前作答的题号:2,4,5 |
| | | |
| | | private var nowVoiceView: View? = null |
| | | |
| | | private var isAutoPlaying = false //true自动播放流程 |
| | | |
| | | var right = true //true进入下一题为全对,false 一旦答错过就置为false,此时进入下一题,本题算做错误 |
| | | |
| | | override fun onFirstVisibleToUser() { |
| | | player.setOnAudioStatusUpdateListener(this) |
| | |
| | | super.handleMessage(msg) |
| | | when(msg.what){ |
| | | PLAY_VOICE->{ |
| | | playing = true |
| | | player.startPlayMusic(requireContext(),data!!.subjectList[group][voiceIndex].correct) |
| | | } |
| | | TO_NEXT->{ |
| | |
| | | } |
| | | } |
| | | PLAY_RIGHT->{ |
| | | playing = true |
| | | rightPlaying = true |
| | | player.startPlayMusic(requireContext(),rightVoice) |
| | | } |
| | | PLAY_ERROR->{ |
| | | playing = true |
| | | errorPlaying = true |
| | | player.startPlayMusic(requireContext(),errorVoice) |
| | | } |
| | |
| | | } |
| | | data?.apply { |
| | | val originList = subjectList[group] |
| | | iv_1.setImageURI(originList[0].img) |
| | | iv_2.setImageURI(originList[1].img) |
| | | iv_4.setImageURI(originList[3].img) |
| | | answerImg.clear() |
| | | answerImg.add(originList[2].img) |
| | | answerImg.add(originList[4].img) |
| | | answerImg.add(originList[5].img) |
| | | cl_voice1_real.postDelayed({ |
| | | iv_1.setImageURI(originList[0].img) |
| | | iv_2.setImageURI(originList[1].img) |
| | | iv_4.setImageURI(originList[3].img) |
| | | // if (cl_1.width > dip(203)){ |
| | | // voiceViews.forEach { |
| | | // it.layoutParams.width = dip(159) |
| | | // it.layoutParams.height = dip(52) |
| | | // it.requestLayout() |
| | | // } |
| | | // } |
| | | voiceViews.forEach { it.visible() } |
| | | },500) |
| | | } |
| | | voiceViews.forEachIndexed { index, constraintLayout -> |
| | | if (index == 0) |
| | |
| | | constraintLayout.isEnabled = data!!.subjectList[group][index].listend |
| | | } |
| | | } |
| | | showVoiceEnable() |
| | | initClick() |
| | | showVoiceEnable() |
| | | isAutoPlaying = true |
| | | voiceViews[0].callOnClick() |
| | | } |
| | | |
| | | private fun initClick() { |
| | | voiceViews.forEachIndexed { index, constraintLayout -> |
| | | constraintLayout.clickDelay { |
| | | constraintLayout.setOnClickListener { |
| | | if (!playing){ |
| | | if (index!=0&&!data!!.subjectList[group][index-1].listend){ |
| | | myToast("请按顺序听语音") |
| | | return@clickDelay |
| | | return@setOnClickListener |
| | | } |
| | | voiceIndex = index |
| | | handler?.sendEmptyMessage(PLAY_VOICE) |
| | |
| | | } |
| | | |
| | | cl_answer_1.clickDelay { |
| | | if (currentImage == 0) |
| | | if (currentImage == 0||!data!!.subjectList[group][currentImage].listend) |
| | | return@clickDelay |
| | | val subject = data!!.subjectList[group][currentImage] |
| | | if (!subject.listend||subject.completed){ |
| | | return@clickDelay |
| | | } |
| | | act.totalCount++ |
| | | data!!.subjectList[group][currentImage].listend = false |
| | | if (answerImg[0] == subject.img){ |
| | | nowVoiceView = voiceViews[currentImage] |
| | | act.rightCount++ |
| | |
| | | motion.transitionToEnd() |
| | | showResultAnim() |
| | | }else{ |
| | | right = false |
| | | handler?.sendEmptyMessage(PLAY_ERROR) |
| | | iv_error_1.visible() |
| | | iv_error_1.postDelayed({ |
| | |
| | | } |
| | | |
| | | cl_answer_2.clickDelay { |
| | | if (currentImage == 0) |
| | | if (currentImage == 0||!data!!.subjectList[group][currentImage].listend) |
| | | return@clickDelay |
| | | val subject = data!!.subjectList[group][currentImage] |
| | | if (!subject.listend||subject.completed){ |
| | |
| | | } |
| | | act.totalCount++ |
| | | |
| | | data!!.subjectList[group][currentImage].listend = false |
| | | if (answerImg[1] == subject.img){ |
| | | nowVoiceView = voiceViews[currentImage] |
| | | act.rightCount++ |
| | |
| | | motion.transitionToEnd() |
| | | showResultAnim() |
| | | }else{ |
| | | right = false |
| | | handler?.sendEmptyMessage(PLAY_ERROR) |
| | | iv_error_2.visible() |
| | | iv_error_2.postDelayed({ |
| | |
| | | } |
| | | |
| | | cl_answer_3.clickDelay { |
| | | if (currentImage == 0) |
| | | if (currentImage == 0||!data!!.subjectList[group][currentImage].listend) |
| | | return@clickDelay |
| | | val subject = data!!.subjectList[group][currentImage] |
| | | if (!subject.listend||subject.completed){ |
| | |
| | | } |
| | | act.totalCount++ |
| | | |
| | | data!!.subjectList[group][currentImage].listend = false |
| | | if (answerImg[2] == subject.img){ |
| | | nowVoiceView = voiceViews[currentImage] |
| | | act.rightCount++ |
| | |
| | | motion.transitionToEnd() |
| | | showResultAnim() |
| | | }else{ |
| | | right = false |
| | | handler?.sendEmptyMessage(PLAY_ERROR) |
| | | iv_error_3.visible() |
| | | iv_error_3.postDelayed({ |
| | |
| | | */ |
| | | fun recover(){ |
| | | Log.e(TAG,"回到上一题,恢复答题前的状态") |
| | | right = true |
| | | isAutoPlaying = false |
| | | motion.progress = 0f |
| | | clearAnswerImg() |
| | | currentImage = 0 |
| | |
| | | override fun onFinishPlay() { |
| | | playing = false |
| | | val subject = data!!.subjectList[group][voiceIndex] |
| | | subject.listend = true |
| | | when(voiceIndex){ |
| | | 0->{ |
| | | iv1_1.visible() |
| | |
| | | }else{ |
| | | hideAnswerImg() |
| | | Log.e(TAG,"本题全部答对,3秒后进入下一图") |
| | | handler?.sendEmptyMessageDelayed(TO_NEXT,3000) |
| | | handler?.sendEmptyMessageDelayed(TO_NEXT,500) //万俊杰:缩短为0.5秒 |
| | | } |
| | | } |
| | | } |
| | | if (errorPlaying) |
| | | errorPlaying = false |
| | | else if (rightPlaying) { |
| | | rightPlaying = false |
| | | data!!.subjectList[group][currentImage].completed = true |
| | | nowVoiceView?.callOnClick() |
| | | }else{ |
| | | subject.listend = true |
| | | if (isAutoPlaying) |
| | | when(voiceIndex){ |
| | | 0-> voiceViews[1].callOnClick() |
| | | 1-> voiceViews[2].callOnClick() |
| | | 3-> voiceViews[4].callOnClick() |
| | | 2,4,5-> isAutoPlaying = false |
| | | } |
| | | else if (subject.completed){ |
| | | when(voiceIndex){ |
| | | 2->{ |
| | | isAutoPlaying = true |
| | | voiceViews[3].callOnClick() |
| | | } |
| | | 4->{ |
| | | voiceViews[5].callOnClick() |
| | | } |
| | | } |
| | | } |
| | | } |
| | | showVoiceEnable() |
| | | |
| | | if (errorPlaying) |
| | | errorPlaying = false |
| | | if (rightPlaying) { |
| | | rightPlaying = false |
| | | data!!.subjectList[group][currentImage].completed = true |
| | | nowVoiceView?.callOnClick() |
| | | } |
| | | } |
| | | |
| | | override fun onPause() { |
| | | super.onPause() |
| | | player.stopPlayMusic() |
| | | } |
| | | |
| | | override fun onDestroy() { |
| | | super.onDestroy() |
| | | handler?.removeCallbacksAndMessages(null) |
| | | } |
| | | } |