pull/32/head
kunfei 5 years ago
parent 43530e2246
commit 9035e15c3e
  1. 20
      app/src/main/java/io/legado/app/help/MediaHelp.kt
  2. 2
      app/src/main/java/io/legado/app/service/ReadAloudService.kt
  3. BIN
      app/src/main/res/raw/silent_sound.mp3

@ -0,0 +1,20 @@
package io.legado.app.help
import android.content.Context
import android.media.MediaPlayer
import io.legado.app.R
object MediaHelp {
fun playSilentSound(mContext: Context) {
try {
// Stupid Android 8 "Oreo" hack to make media buttons work
val mMediaPlayer = MediaPlayer.create(mContext, R.raw.silent_sound)
mMediaPlayer.setOnCompletionListener { it.release() }
mMediaPlayer.start()
} catch (ignored: Exception) {
}
}
}

@ -17,6 +17,7 @@ import io.legado.app.base.BaseService
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.constant.Bus import io.legado.app.constant.Bus
import io.legado.app.constant.Status import io.legado.app.constant.Status
import io.legado.app.help.MediaHelp
import io.legado.app.help.PendingIntentHelp import io.legado.app.help.PendingIntentHelp
import io.legado.app.receiver.MediaButtonReceiver import io.legado.app.receiver.MediaButtonReceiver
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
@ -206,6 +207,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
* @return 音频焦点 * @return 音频焦点
*/ */
private fun requestFocus(): Boolean { private fun requestFocus(): Boolean {
MediaHelp.playSilentSound(this)
val request: Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val request: Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
audioManager.requestAudioFocus(mFocusRequest) audioManager.requestAudioFocus(mFocusRequest)
} else { } else {

Loading…
Cancel
Save