Ver Fonte

1.优化进入页面崩溃bug

王鹏鹏 há 1 ano atrás
pai
commit
effbe63371

+ 4 - 77
baselib/src/main/java/com/yingyangfly/baselib/dialog/TaskFragment.kt

@@ -3,7 +3,6 @@ package com.yingyangfly.baselib.dialog
 import android.annotation.SuppressLint
 import android.content.Context
 import android.os.Bundle
-import android.text.TextUtils
 import android.view.LayoutInflater
 import android.view.MotionEvent
 import android.view.View
@@ -16,17 +15,10 @@ import com.bumptech.glide.Glide
 import com.gyf.immersionbar.BarHide
 import com.gyf.immersionbar.ktx.immersionBar
 import com.yingyangfly.baselib.R
-import com.yingyangfly.baselib.db.AppDataBase
-import com.yingyangfly.baselib.db.VoicePlayerBean
-import com.yingyangfly.baselib.db.VoicePlayerDao
 import com.yingyangfly.baselib.ext.setOnSingleClickListener
-import com.yingyangfly.baselib.ext.toast
-import com.yingyangfly.baselib.net.BaseObserver
-import com.yingyangfly.baselib.net.BaselibServiceFactory
-import com.yingyangfly.baselib.net.MyRxScheduler
-import com.yingyangfly.baselib.player.VoicePlayer
+import com.yingyangfly.baselib.utils.LiveEventBusUtil
+import com.yingyangfly.baselib.utils.RxBusCodes
 import com.yingyangfly.baselib.utils.ViewTool
-import io.reactivex.schedulers.Schedulers
 
 /**
  * 任务提醒
@@ -40,13 +32,6 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
     private var id = ""
     private var content: Context? = null
 
-    /**
-     * 语音合成
-     */
-    private var voicePlayer: VoicePlayer? = null
-    private var db: AppDataBase? = null
-    private var dao: VoicePlayerDao? = null
-
     var onDialogClickListener: ((bean: String) -> Unit)? = null
 
     fun setTaskDesn(taskDesn: String, id: String) {
@@ -82,11 +67,6 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
             activity, R.layout.fragment_task, container, 1194, 834
         )
         content = activity
-        voicePlayer = VoicePlayer.getInstance(content)
-        db = AppDataBase.getInstance(content!!.applicationContext)
-        if (db != null) {
-            dao = db?.getVoicePlayerDao()
-        }
         findId(rootView)
         init()
         return rootView
@@ -115,41 +95,7 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
      * 播放语音合成内容
      */
     private fun speak(taskDesn: String) {
-        if (voicePlayer != null && voicePlayer?.isPlaying == false) {
-            if (dao != null) {
-                val voicePlayerBean = dao?.getVoicePlayerBean(taskDesn)
-                if (voicePlayerBean != null && voicePlayer != null) {
-                    voicePlayer?.play(voicePlayerBean.url) {
-
-                    }
-                } else {
-                    getWordsUrl(taskDesn)
-                }
-            } else {
-                getWordsUrl(taskDesn)
-            }
-        }
-    }
-
-    /**
-     * 获取声音 url
-     */
-    private fun getWordsUrl(word: String) {
-        BaselibServiceFactory.getService()
-            .getVoiceUrl(word)
-            .subscribeOn(Schedulers.io())
-            .compose(MyRxScheduler.ioMain(content!!, false))
-            .subscribe(object : BaseObserver<String>() {
-                override fun onSuccess(t: String?) {
-                    if (TextUtils.isEmpty(t).not()) {
-                        setData(t!!)
-                    }
-                }
-
-                override fun onFail(msg: String) {
-                    msg.toast()
-                }
-            })
+        LiveEventBusUtil.send(RxBusCodes.SPEECHSYNTHESIS, taskDesn)
     }
 
     @SuppressLint("ClickableViewAccessibility")
@@ -162,26 +108,7 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
     }
 
     override fun dismiss() {
-        if (voicePlayer != null) {
-            if (voicePlayer!!.isPlaying) {
-                voicePlayer?.stop()
-            }
-        }
+        LiveEventBusUtil.send(RxBusCodes.STOPVOICE, "")
         super.dismiss()
     }
-
-    private fun setData(t: String) {
-        confirmBtn!!.postDelayed({
-            if (dao != null) {
-                val voicePlayerBean = VoicePlayerBean().apply {
-                    url = t
-                    words = taskDesn
-                }
-                dao?.insert(voicePlayerBean)
-            }
-            voicePlayer?.play(t) {
-
-            }
-        }, 300)
-    }
 }