|
|
|
@ -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,21 +37,48 @@ class HttpReadAloudService : BaseReadAloudService(), |
|
|
|
|
mediaPlayer.release() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun getAudioPath(): String { |
|
|
|
|
val audioText = contentList[nowSpeak] |
|
|
|
|
private fun getAudioBody(): Map<String, String> { |
|
|
|
|
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 |
|
|
|
|
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(getAudioPath()) |
|
|
|
|
mediaPlayer.setDataSource(FileInputStream(file).fd) |
|
|
|
|
mediaPlayer.prepareAsync() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun pauseReadAloud(pause: Boolean) { |
|
|
|
|
super.pauseReadAloud(pause) |
|
|
|
|