Przeglądaj źródła

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

王鹏鹏 2 lat temu
rodzic
commit
2099c9ed0d

+ 2 - 5
baselib/src/main/java/com/yingyangfly/baselib/player/VoicePlayer.java

@@ -33,6 +33,7 @@ public class VoicePlayer {
         Context baseContext = cxt.getApplicationContext();
         audioManager = (AudioManager) baseContext.getSystemService(Context.AUDIO_SERVICE);
         mediaPlayer = new MediaPlayer();
+        setSpeaker();
     }
 
     public MediaPlayer getPlayer() {
@@ -44,13 +45,9 @@ public class VoicePlayer {
     }
 
     public void play(final String url, final MediaPlayer.OnCompletionListener listener) {
-        if (mediaPlayer.isPlaying()) {
-            stop();
-        }
         isPause = false;
         onCompletionListener = listener;
         try {
-            setSpeaker();
             if (!TextUtils.isEmpty(url)) {
                 mediaPlayer.setDataSource(url);
                 mediaPlayer.prepareAsync();
@@ -93,7 +90,7 @@ public class VoicePlayer {
     private void setSpeaker() {
         audioManager.setMode(AudioManager.MODE_NORMAL);
         audioManager.setSpeakerphoneOn(true);
-        mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
+        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
     }
 
 }

+ 70 - 8
home/src/main/java/com/yingyangfly/home/activity/HomeActivity.kt

@@ -8,17 +8,18 @@ import androidx.recyclerview.widget.GridLayoutManager
 import com.alibaba.android.arouter.facade.annotation.Route
 import com.yingyang.home.R
 import com.yingyang.home.databinding.ActivityHomeBinding
+import com.yingyangfly.baselib.db.VoicePlayerBean
 import com.yingyangfly.baselib.dialog.TaskFragment
 import com.yingyangfly.baselib.dialog.TipsDialog
 import com.yingyangfly.baselib.ext.*
 import com.yingyangfly.baselib.guideview.Guide
 import com.yingyangfly.baselib.guideview.GuideBuilder
 import com.yingyangfly.baselib.mvvm.BaseMVVMActivity
+import com.yingyangfly.baselib.player.VoicePlayer
 import com.yingyangfly.baselib.router.RouterUrlCommon
 import com.yingyangfly.baselib.utils.AppUtil
 import com.yingyangfly.baselib.utils.JumpUtil
 import com.yingyangfly.baselib.utils.User
-import com.yingyangfly.baselib.voice.PlayVoice
 import com.yingyangfly.home.adapter.GameAdapter
 import com.yingyangfly.home.component.ShowFunTimeViewComponent
 import com.yingyangfly.home.component.ShowHealthCounselingViewComponent
@@ -50,11 +51,16 @@ class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(),
     private var pageList = mutableListOf<Record>()
     private val gameAdapter by lazy { GameAdapter() }
 
-    private var playVoice: PlayVoice? = null
+    //    private var playVoice: PlayVoice? = null
+    /**
+     * 语音合成
+     */
+    private var voicePlayer: VoicePlayer? = null
 
     override fun initViews() {
-        playVoice = PlayVoice()
-        playVoice?.setContext(mContext)
+        voicePlayer = VoicePlayer.getInstance(mContext)
+//        playVoice = PlayVoice()
+//        playVoice?.setContext(mContext)
         binding {
             rvGame.layoutManager = GridLayoutManager(this@HomeActivity, 2)
             rvGame.adapter = gameAdapter
@@ -239,7 +245,8 @@ class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(),
                                     .not()
                             ) {
                                 User.saveTaskDialogStatus(User.getNowDay())
-                                playVoice?.speak(it.taskDesn)
+                                speak(it.taskDesn)
+//                                playVoice?.speak(it.taskDesn)
                             }
                         }
                     } else {
@@ -250,7 +257,8 @@ class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(),
                         }
                         if (TextUtils.equals(User.getNowDay(), User.getTaskDialogStatus()).not()) {
                             User.saveTaskDialogStatus(User.getNowDay())
-                            playVoice?.speak(it.taskDesn)
+                            speak(it.taskDesn)
+//                            playVoice?.speak(it.taskDesn)
                         }
                     }
                 }
@@ -261,6 +269,58 @@ class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(),
         })
     }
 
+    /**
+     * 语音合成
+     */
+    private fun speak(taskDesn: String) {
+        if (db != null) {
+            val voicePlayerDao = db?.getVoicePlayerDao()
+            if (voicePlayerDao != null) {
+                val voicePlayerBean = voicePlayerDao.getVoicePlayerBean(taskDesn)
+                if (voicePlayerBean != null) {
+                    if (voicePlayer != null) {
+                        voicePlayer?.play(voicePlayerBean.url) {
+
+                        }
+                    }
+                } else {
+                    getVoiceUrl(taskDesn)
+                }
+            } else {
+                getVoiceUrl(taskDesn)
+            }
+        } else {
+            getVoiceUrl(taskDesn)
+        }
+    }
+
+    /**
+     * 获取声音url
+     */
+    private fun getVoiceUrl(taskDesn: String) {
+        viewModel.getVoiceUrl(taskDesn, fail = {
+            it.toast()
+        }, success = {
+            if (TextUtils.isEmpty(it).not()) {
+                if (db != null) {
+                    val voicePlayerDao = db?.getVoicePlayerDao()
+                    if (voicePlayerDao != null) {
+                        val voicePlayerBean = VoicePlayerBean().apply {
+                            url = it
+                            words = taskDesn
+                        }
+                        voicePlayerDao.insert(voicePlayerBean)
+                    }
+                }
+                if (voicePlayer != null) {
+                    voicePlayer?.play(it) {
+
+                    }
+                }
+            }
+        })
+    }
+
     /**
      * 训练完成弹窗页面
      */
@@ -453,8 +513,10 @@ class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(),
     }
 
     override fun onDestroy() {
-        if (playVoice != null) {
-            playVoice?.stop()
+        if (voicePlayer != null) {
+            if (voicePlayer!!.isPlaying) {
+                voicePlayer?.stop()
+            }
         }
         super.onDestroy()
     }

+ 15 - 0
home/src/main/java/com/yingyangfly/home/activity/HomeViewModel.kt

@@ -103,4 +103,19 @@ class HomeViewModel : BaseViewModel() {
         success,
         fail
     )
+
+    /**
+     * 获取声音url
+     */
+    fun getVoiceUrl(
+        msg: String,
+        fail: ((msg: String) -> Unit)? = null,
+        success: ((success: String?) -> Unit)? = null,
+    ) = launchFlow(false) {
+        HOME_API.getVoiceUrl(msg)
+    }.runUI(
+        success,
+        fail
+    )
+
 }

+ 8 - 0
home/src/main/java/com/yingyangfly/home/net/HomeApiService.kt

@@ -57,4 +57,12 @@ interface HomeApiService {
     @POST("app/review/findAllReview")
     suspend fun getQuestions(@Body requestBody: RequestBody): BaseResp<List<QuestionsBean>>
 
+    /**
+     * 获取声音url
+     */
+    @POST("app/video/getVoiceUrl")
+    suspend fun getVoiceUrl(
+        @Query("voiceMsg") voiceMsg: String
+    ): BaseResp<String>
+
 }

+ 64 - 7
webview/src/main/java/com/yingyangfly/webview/BridgeWebActivity.kt

@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
 import android.content.pm.ActivityInfo
 import android.os.Build
 import android.os.Bundle
+import android.text.TextUtils
 import android.view.ViewGroup
 import android.webkit.WebResourceRequest
 import android.webkit.WebSettings
@@ -14,12 +15,20 @@ import androidx.databinding.DataBindingUtil
 import com.alibaba.android.arouter.facade.annotation.Route
 import com.gyf.immersionbar.BarHide
 import com.gyf.immersionbar.ktx.immersionBar
+import com.yingyangfly.baselib.db.AppDataBase
+import com.yingyangfly.baselib.db.VoicePlayerBean
+import com.yingyangfly.baselib.db.VoicePlayerDao
+import com.yingyangfly.baselib.ext.toast
+import com.yingyangfly.baselib.net.BaseObserver
+import com.yingyangfly.baselib.net.MyRxScheduler
+import com.yingyangfly.baselib.player.VoicePlayer
 import com.yingyangfly.baselib.router.RouterUrlCommon
 import com.yingyangfly.baselib.utils.LiveEventBusUtil
 import com.yingyangfly.baselib.utils.RxBusCodes
 import com.yingyangfly.baselib.utils.ViewTool
-import com.yingyangfly.baselib.voice.PlayVoice
 import com.yingyangfly.webview.databinding.ActivityBridgeWebBinding
+import com.yingyangfly.webview.net.WebViewServiceFactory
+import io.reactivex.schedulers.Schedulers
 
 /**
  * BridgeWebview
@@ -30,7 +39,9 @@ class BridgeWebActivity : AppCompatActivity() {
     private lateinit var binding: ActivityBridgeWebBinding
     private lateinit var webSettings: WebSettings
     private var url: String = ""
-    private var playVoice: PlayVoice? = null
+    private var db: AppDataBase? = null
+    private var dao: VoicePlayerDao? = null
+    private var voicePlayer: VoicePlayer? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -38,8 +49,11 @@ class BridgeWebActivity : AppCompatActivity() {
             hideBar(BarHide.FLAG_HIDE_BAR)
             navigationBarColor(com.yingyangfly.baselib.R.color.transparent)
         }
-        playVoice = PlayVoice()
-        playVoice?.setContext(this)
+        db = AppDataBase.getInstance(this)
+        if (db != null) {
+            dao = db?.getVoicePlayerDao()
+        }
+        voicePlayer = VoicePlayer.getInstance(this)
         url = intent.getStringExtra("url") ?: ""
         binding = DataBindingUtil.setContentView(this, R.layout.activity_bridge_web)
         ViewTool.inflateLayoutPixels(this, binding.root, 1194, 834)
@@ -116,7 +130,18 @@ class BridgeWebActivity : AppCompatActivity() {
                 val msg = it.split("voice:")
                 if (msg.isNullOrEmpty().not()) {
                     if (msg[1].isNullOrEmpty().not()) {
-                        playVoice?.speak(msg[1])
+                        if (dao != null) {
+                            val voicePlayerBean = dao?.getVoicePlayerBean(msg[1])
+                            if (voicePlayerBean != null && TextUtils.isEmpty(voicePlayerBean.url)
+                                    .not()
+                            ) {
+                                voicePlayer?.play(voicePlayerBean.url) {
+
+                                }
+                            } else {
+                                getWordsUrl(msg[1])
+                            }
+                        }
                     }
                 }
             }
@@ -124,10 +149,42 @@ class BridgeWebActivity : AppCompatActivity() {
         }
     }
 
+    /**
+     * 获取声音 url
+     */
+    private fun getWordsUrl(word: String) {
+        WebViewServiceFactory.getService()
+            .getVoiceUrl(word)
+            .subscribeOn(Schedulers.io())
+            .compose(MyRxScheduler.ioMain(this, false))
+            .subscribe(object : BaseObserver<String>() {
+                override fun onSuccess(t: String?) {
+                    t?.let {
+                        if (dao != null) {
+                            val voicePlayerBean = VoicePlayerBean().apply {
+                                words = word
+                                url = it
+                            }
+                            dao?.insert(voicePlayerBean)
+                        }
+                        voicePlayer?.play(it) {
+
+                        }
+                    }
+                }
+
+                override fun onFail(msg: String) {
+                    msg.toast()
+                }
+            })
+    }
+
     override fun onDestroy() {
         destoryWebView()
-        if (playVoice != null) {
-            playVoice?.stop()
+        if (voicePlayer != null) {
+            if (voicePlayer!!.isPlaying) {
+                voicePlayer?.stop()
+            }
         }
         super.onDestroy()
     }

+ 22 - 0
webview/src/main/java/com/yingyangfly/webview/net/WebViewApiService.kt

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

+ 21 - 0
webview/src/main/java/com/yingyangfly/webview/net/WebViewServiceFactory.kt

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