diff --git a/app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt b/app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt index 0aa3ef3db..92b5f1cf3 100644 --- a/app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt +++ b/app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt @@ -42,4 +42,12 @@ interface IHttpPostApi { @Body body: RequestBody, @HeaderMap headers: Map ): Call + + @FormUrlEncoded + @POST + fun postMapByte( + @Url url: String, + @FieldMap(encoded = true) fieldMap: Map, + @HeaderMap headers: Map + ): Call } diff --git a/app/src/main/java/io/legado/app/help/http/ByteConverter.kt b/app/src/main/java/io/legado/app/help/http/ByteConverter.kt new file mode 100644 index 000000000..0f1682274 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/http/ByteConverter.kt @@ -0,0 +1,20 @@ +package io.legado.app.help.http + +import okhttp3.ResponseBody +import retrofit2.Converter +import retrofit2.Retrofit +import java.lang.reflect.Type + +class ByteConverter() : Converter.Factory() { + + override fun responseBodyConverter( + type: Type?, + annotations: Array?, + retrofit: Retrofit? + ): Converter? { + return Converter { value -> + value.bytes() + } + } + +} diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index 2945d2882..eeb54986e 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -51,6 +51,15 @@ object HttpHelper { .build() } + fun getByteRetrofit(baseUrl: String): Retrofit { + return Retrofit.Builder().baseUrl(baseUrl) + .addConverterFactory(ByteConverter()) + //增加返回值为Observable的支持 + .addCallAdapterFactory(CoroutinesCallAdapterFactory.create()) + .client(client) + .build() + } + private fun getHeaderInterceptor(): Interceptor { return Interceptor { chain -> val request = chain.request() diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index efdb0397e..13dbfad04 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -42,7 +42,7 @@ class AnalyzeRule(private var book: BaseBook? = null) { if (content == null) throw AssertionError("Content cannot be null") isJSON = content.toString().isJson() this.content = content - this.baseUrl = baseUrl?.split("\n".toRegex(), 1)?.get(0) + this.baseUrl = baseUrl objectChangedXP = true objectChangedJS = true objectChangedJP = true @@ -150,19 +150,17 @@ class AnalyzeRule(private var book: BaseBook? = null) { if (result is String) { result = listOf((result as String).htmlFormat().split("\n")) } - baseUrl?.let { - if (isUrl && !TextUtils.isEmpty(it)) { - val urlList = ArrayList() - if (result is List<*>) { - for (url in result as List<*>) { - val absoluteURL = NetworkUtils.getAbsoluteURL(it, url.toString()) - if (!urlList.contains(absoluteURL)) { - urlList.add(absoluteURL) - } + if (isUrl) { + val urlList = ArrayList() + if (result is List<*>) { + for (url in result as List<*>) { + val absoluteURL = NetworkUtils.getAbsoluteURL(baseUrl, url.toString()) + if (!absoluteURL.isNullOrEmpty() && !urlList.contains(absoluteURL)) { + urlList.add(absoluteURL) } } - return urlList } + return urlList } @Suppress("UNCHECKED_CAST") return result as? List @@ -212,11 +210,9 @@ class AnalyzeRule(private var book: BaseBook? = null) { } } } - if (result == null) return "" - baseUrl?.let { - return if (isUrl) { - NetworkUtils.getAbsoluteURL(it, result.toString()) - } else result.toString() + if (result == null) result = "" + if (isUrl) { + return NetworkUtils.getAbsoluteURL(baseUrl, result.toString()) ?: "" } return result.toString() } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 714b49418..8c9730fa8 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -255,23 +255,20 @@ class AnalyzeUrl( return when { method == Method.POST -> { if (fieldMap.isNotEmpty()) { - HttpHelper.getApiService( - baseUrl - ).postMap(url, fieldMap, headerMap) + HttpHelper + .getApiService(baseUrl) + .postMap(url, fieldMap, headerMap) } else { - HttpHelper.getApiService( - baseUrl - ).postBody( - url, - body, - headerMap - ) + HttpHelper + .getApiService(baseUrl) + .postBody(url, body, headerMap) } } - fieldMap.isEmpty() -> HttpHelper.getApiService( - baseUrl - ).get(url, headerMap) - else -> HttpHelper.getApiService(baseUrl) + fieldMap.isEmpty() -> HttpHelper + .getApiService(baseUrl) + .get(url, headerMap) + else -> HttpHelper + .getApiService(baseUrl) .getMap(url, fieldMap, headerMap) } } @@ -280,23 +277,20 @@ class AnalyzeUrl( return when { method == Method.POST -> { if (fieldMap.isNotEmpty()) { - HttpHelper.getApiService( - baseUrl - ).postMapAsync(url, fieldMap, headerMap) + HttpHelper + .getApiService(baseUrl) + .postMapAsync(url, fieldMap, headerMap) } else { - HttpHelper.getApiService( - baseUrl - ).postBodyAsync( - url, - body, - headerMap - ) + HttpHelper + .getApiService(baseUrl) + .postBodyAsync(url, body, headerMap) } } - fieldMap.isEmpty() -> HttpHelper.getApiService( - baseUrl - ).getAsync(url, headerMap) - else -> HttpHelper.getApiService(baseUrl) + fieldMap.isEmpty() -> HttpHelper + .getApiService(baseUrl) + .getAsync(url, headerMap) + else -> HttpHelper + .getApiService(baseUrl) .getMapAsync(url, fieldMap, headerMap) } } diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt index 2dde06378..fb25b4e2d 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -3,10 +3,20 @@ package io.legado.app.service import android.app.PendingIntent import android.media.MediaPlayer import io.legado.app.constant.Bus +import io.legado.app.data.api.IHttpPostApi +import io.legado.app.help.FileHelp import io.legado.app.help.IntentHelp +import io.legado.app.help.http.HttpHelper import io.legado.app.utils.getPrefInt import io.legado.app.utils.getPrefString import io.legado.app.utils.postEvent +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.Dispatchers.Main +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import org.jetbrains.anko.toast +import java.io.File +import java.io.FileInputStream class HttpReadAloudService : BaseReadAloudService(), MediaPlayer.OnPreparedListener, @@ -27,19 +37,46 @@ class HttpReadAloudService : BaseReadAloudService(), mediaPlayer.release() } - private fun getAudioPath(): String { - val audioText = contentList[nowSpeak] + private fun getAudioBody(): Map { val spd = (getPrefInt("ttsSpeechRate", 25) + 5) / 5 val per = getPrefString("ttsSpeechPer") ?: "0" - return "http://tts.baidu.com/text2audio?idx=1&tex=$audioText&cuid=baidu_speech_demo&cod=2&lan=zh&ctp=1&pdt=1&spd=$spd&per=$per&vol=5&pit=5&_res_tag_=audio" + return mapOf( + Pair("idx", "1"), + Pair("tex", contentList[nowSpeak]), + Pair("cuid", "baidu_speech_demo "), + Pair("cod", "2"), + Pair("lan", "zh"), + Pair("ctp", "1"), + Pair("pdt", "1"), + Pair("spd", spd.toString()), + Pair("per", per), + Pair("vol", "5"), + Pair("pit", "5"), + Pair("_res_tag_", "audio") + ) } override fun play() { if (contentList.isEmpty()) return - if (requestFocus()) { - mediaPlayer.reset() - mediaPlayer.setDataSource(getAudioPath()) - mediaPlayer.prepareAsync() + launch(IO) { + if (requestFocus()) { + val bytes = HttpHelper.getByteRetrofit("http://tts.baidu.com") + .create(IHttpPostApi::class.java) + .postMapByte("http://tts.baidu.com/text2audio", getAudioBody(), mapOf()) + .execute().body() + if (bytes == null) { + withContext(Main) { + toast("访问失败") + } + } else { + val file = + FileHelp.getFile(cacheDir.absolutePath + File.separator + "bdTts.mp3") + file.writeBytes(bytes) + mediaPlayer.reset() + mediaPlayer.setDataSource(FileInputStream(file).fd) + mediaPlayer.prepareAsync() + } + } } } diff --git a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt index 7b1b23b32..3f42784f9 100644 --- a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt +++ b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt @@ -1,6 +1,5 @@ package io.legado.app.utils -import android.text.TextUtils import retrofit2.Response import java.net.URL import java.util.* @@ -71,11 +70,12 @@ object NetworkUtils { /** * 获取绝对地址 */ - fun getAbsoluteURL(baseURL: String, relativePath: String): String { + fun getAbsoluteURL(baseURL: String?, relativePath: String?): String? { + if (baseURL.isNullOrEmpty()) return relativePath + if (relativePath.isNullOrEmpty()) return baseURL var relativeUrl = relativePath - if (TextUtils.isEmpty(baseURL)) return relativePath try { - val absoluteUrl = URL(baseURL) + val absoluteUrl = URL(baseURL.substringBefore(",")) val parseUrl = URL(absoluteUrl, relativePath) relativeUrl = parseUrl.toString() return relativeUrl