Kaynağa Gözat

修复在线tts朗读失败bug

kunfei 3 yıl önce
ebeveyn
işleme
f4e5eedb37

+ 26 - 0
app/src/androidTest/java/io/legado/app/HttpTtsTest.kt

@@ -0,0 +1,26 @@
+package io.legado.app
+
+import io.legado.app.help.config.AppConfig
+import io.legado.app.model.analyzeRule.AnalyzeUrl
+import kotlinx.coroutines.runBlocking
+import org.junit.Test
+
+class HttpTtsTest {
+
+    @Test
+    fun test() {
+        val url = """
+            http://tsn.baidu.com/text2audio,{
+                "method": "POST",
+                "body": "tex={{java.encodeURI(java.encodeURI(speakText))}}&spd={{(speakSpeed + 5) / 10 + 4}}&per=4114&cuid=baidu_speech_demo&idx=1&cod=2&lan=zh&ctp=1&pdt=220&vol=5&aue=6&pit=5&_res_tag_=audio"
+            }
+        """.trimIndent()
+        val analyzeUrl =
+            AnalyzeUrl(url, speakText = "魔神", speakSpeed = AppConfig.speechRatePlay + 5)
+        runBlocking {
+            val response = analyzeUrl.getResponseAwait()
+            response.headers
+        }
+    }
+
+}

+ 7 - 12
app/src/main/java/io/legado/app/help/exoplayer/ExoPlayerHelper.kt

@@ -59,7 +59,7 @@ object ExoPlayerHelper {
         return MediaItem.Builder().setUri(formatUrl).build()
     }
 
-    fun createExoPlayer(context: Context): ExoPlayer {
+    fun createHttpExoPlayer(context: Context): ExoPlayer {
         return ExoPlayer.Builder(context).setLoadControl(
             DefaultLoadControl.Builder().setBufferDurationsMs(
                 DefaultLoadControl.DEFAULT_MIN_BUFFER_MS,
@@ -68,12 +68,11 @@ object ExoPlayerHelper {
                 DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS / 10
             ).build()
 
-        )
-            .setMediaSourceFactory(
-                DefaultMediaSourceFactory(context)
-                    .setDataSourceFactory(resolvingDataSource)
-                    .setLiveTargetOffsetMs(5000)
-            ).build()
+        ).setMediaSourceFactory(
+            DefaultMediaSourceFactory(context)
+                .setDataSourceFactory(resolvingDataSource)
+                .setLiveTargetOffsetMs(5000)
+        ).build()
     }
 
     /**
@@ -107,9 +106,7 @@ object ExoPlayerHelper {
 
 
     private val resolvingDataSource: ResolvingDataSource.Factory by lazy {
-        ResolvingDataSource.Factory(
-            cacheDataSourceFactory
-        ) {
+        ResolvingDataSource.Factory(cacheDataSourceFactory) {
             var res = it
 
             if (it.uri.toString().contains(SPLIT_TAG)) {
@@ -123,8 +120,6 @@ object ExoPlayerHelper {
                 }
             }
 
-
-
             res
 
         }

+ 1 - 8
app/src/main/java/io/legado/app/service/AudioPlayService.kt

@@ -74,7 +74,7 @@ class AudioPlayService : BaseService(),
         MediaHelp.buildAudioFocusRequestCompat(this)
     }
     private val exoPlayer: ExoPlayer by lazy {
-       ExoPlayerHelper.createExoPlayer(this)
+       ExoPlayerHelper.createHttpExoPlayer(this)
     }
     private var mediaSessionCompat: MediaSessionCompat? = null
     private var broadcastReceiver: BroadcastReceiver? = null
@@ -152,13 +152,6 @@ class AudioPlayService : BaseService(),
                     chapter = AudioPlay.durChapter,
                     headerMapF = AudioPlay.headers(true),
                 )
-                //val uri = Uri.parse(analyzeUrl.url)
-                //ExoPlayerHelper.preDownload(uri, analyzeUrl.headerMap)
-                //休息1秒钟,防止403
-                //delay(1000)
-//                val mediaSource = ExoPlayerHelper
-//                    .createMediaSource(uri, analyzeUrl.headerMap)
-                //exoPlayer.setMediaSource(mediaSource)
                 exoPlayer.setMediaItem(ExoPlayerHelper.createMediaItem(analyzeUrl.url,analyzeUrl.headerMap))
                 exoPlayer.playWhenReady = true
                 exoPlayer.prepare()

+ 17 - 8
app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

@@ -16,13 +16,21 @@ import io.legado.app.exception.ConcurrentException
 import io.legado.app.exception.NoStackTraceException
 import io.legado.app.help.config.AppConfig
 import io.legado.app.help.coroutine.Coroutine
-import io.legado.app.help.exoplayer.ExoPlayerHelper
 import io.legado.app.model.ReadAloud
 import io.legado.app.model.ReadBook
 import io.legado.app.model.analyzeRule.AnalyzeUrl
-import io.legado.app.utils.*
-import kotlinx.coroutines.*
+import io.legado.app.utils.FileUtils
+import io.legado.app.utils.MD5Utils
+import io.legado.app.utils.postEvent
+import io.legado.app.utils.printOnDebug
+import io.legado.app.utils.servicePendingIntent
+import io.legado.app.utils.toastOnUi
+import kotlinx.coroutines.CancellationException
 import kotlinx.coroutines.Dispatchers.IO
+import kotlinx.coroutines.Job
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.ensureActive
+import kotlinx.coroutines.launch
 import kotlinx.coroutines.sync.Mutex
 import kotlinx.coroutines.sync.withLock
 import okhttp3.Response
@@ -39,8 +47,7 @@ class HttpReadAloudService : BaseReadAloudService(),
     Player.Listener {
 
     private val exoPlayer: ExoPlayer by lazy {
-        //ExoPlayer.Builder(this).build()
-        ExoPlayerHelper.createExoPlayer(this)
+        ExoPlayer.Builder(this).build()
     }
     private val ttsFolderPath: String by lazy {
         cacheDir.absolutePath + File.separator + "httpTTS" + File.separator
@@ -161,9 +168,11 @@ class HttpReadAloudService : BaseReadAloudService(),
                 if (checkJs?.isNotBlank() == true) {
                     response = analyzeUrl.evalJS(checkJs, response) as Response
                 }
-                val ct = httpTts.contentType
-                if (ct?.isNotBlank() == true) {
-                    response.headers["Content-Type"]?.let { contentType ->
+                response.headers["Content-Type"]?.let { contentType ->
+                    val ct = httpTts.contentType
+                    if (contentType == "application/json") {
+                        throw NoStackTraceException(response.body!!.string())
+                    } else if (ct?.isNotBlank() == true) {
                         if (!contentType.matches(ct.toRegex())) {
                             throw NoStackTraceException("TTS服务器返回错误:" + response.body!!.string())
                         }