Преглед на файлове

1.添加从后台获取百度语音合成接口功能

王鹏鹏 преди 2 години
родител
ревизия
678eacd87c

+ 87 - 7
baselib/src/main/java/com/yingyangfly/baselib/dialog/TaskFragment.kt

@@ -1,5 +1,6 @@
 package com.yingyangfly.baselib.dialog
 
+import android.content.Context
 import android.graphics.drawable.AnimationDrawable
 import android.os.Bundle
 import android.view.LayoutInflater
@@ -13,9 +14,16 @@ import androidx.fragment.app.DialogFragment
 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.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.ViewTool
-import com.yingyangfly.baselib.voice.PlayVoice
+import io.reactivex.schedulers.Schedulers
 
 /**
  * 任务提醒
@@ -28,7 +36,13 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
     private var confirmBtan: AppCompatButton? = null
     private var animationDrawable: AnimationDrawable? = null
     private var id = ""
-    private var playVoice: PlayVoice? = null
+//    private var playVoice: PlayVoice? = null
+    /**
+     * 语音合成
+     */
+    private var voicePlayer: VoicePlayer? = null
+
+    private var content: Context? = null
 
     var onDialogClickListener: ((bean: String) -> Unit)? = null
 
@@ -61,11 +75,13 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
             hideBar(BarHide.FLAG_HIDE_BAR)
             navigationBarColor(R.color.transparent)
         }
+        content = activity
         val rootView = ViewTool.inflateFragmentPixels(
             activity, R.layout.fragment_task, container, 1194, 834
         )
-        playVoice = PlayVoice()
-        playVoice?.setContext(requireActivity())
+        voicePlayer = VoicePlayer.getInstance(activity)
+//        playVoice = PlayVoice()
+//        playVoice?.setContext(requireActivity())
         findId(rootView)
         init()
         return rootView
@@ -92,7 +108,66 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
         animationDrawable = fishImage?.drawable as AnimationDrawable?
         animationDrawable?.start()
         tvContent?.text = taskDesn
-        playVoice?.speak(taskDesn)
+        speak(taskDesn)
+//        playVoice?.speak(taskDesn)
+    }
+
+    /**
+     * 播放语音合成内容
+     */
+    private fun speak(taskDesn: String) {
+        if (content != null) {
+            val db = AppDataBase.getInstance(content!!)
+            if (db != null) {
+                val voicePlayerDao = db.getVoicePlayerDao()
+                if (voicePlayerDao != null) {
+                    val voicePlayerBean = voicePlayerDao.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?) {
+                    t?.let {
+                        val db = AppDataBase.getInstance(content!!)
+                        if (db != null) {
+                            val voicePlayerDao = db.getVoicePlayerDao()
+                            if (voicePlayerDao != null) {
+                                val voicePlayerBean = VoicePlayerBean().apply {
+                                    url = it
+                                    words = taskDesn
+                                }
+                                voicePlayerDao.insert(voicePlayerBean)
+                            }
+                        }
+                        voicePlayer?.play(it) {
+
+                        }
+                    }
+                }
+
+                override fun onFail(msg: String) {
+                    msg.toast()
+                }
+            })
     }
 
     override fun onTouch(v: View?, event: MotionEvent?): Boolean {
@@ -104,8 +179,13 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
     }
 
     override fun dismiss() {
-        if (playVoice != null) {
-            playVoice?.stop()
+//        if (playVoice != null) {
+//            playVoice?.stop()
+//        }
+        if (voicePlayer != null) {
+            if (voicePlayer!!.isPlaying) {
+                voicePlayer?.stop()
+            }
         }
         super.dismiss()
     }

+ 21 - 0
baselib/src/main/java/com/yingyangfly/baselib/net/BaselibApiService.kt

@@ -0,0 +1,21 @@
+package com.yingyangfly.baselib.net
+
+import io.reactivex.Observable
+import retrofit2.http.POST
+import retrofit2.http.Query
+
+/**
+ * Author: Austin
+ * Date: 19-4-1
+ * Description:
+ */
+interface BaselibApiService {
+
+    /**
+     * 获取声音 url
+     */
+    @POST("app/video/getVoiceUrl")
+    fun getVoiceUrl(
+        @Query("voiceMsg") voiceMsg: String
+    ): Observable<XBaseEntity<String>>
+}

+ 20 - 0
baselib/src/main/java/com/yingyangfly/baselib/net/BaselibServiceFactory.kt

@@ -0,0 +1,20 @@
+package com.yingyangfly.baselib.net
+
+import com.yingyangfly.baselib.config.AccountConfig
+
+/**
+ * Author: Austin
+ * Date: 19-4-11
+ * Description:
+ */
+object BaselibServiceFactory {
+
+    fun getService(): BaselibApiService {
+        if (apiService != null) {
+            return apiService!!
+        }
+        return ServiceFactory.getService(AccountConfig.API_URL, BaselibApiService::class.java)
+    }
+
+    var apiService: BaselibApiService? = null
+}