pull/32/head
kunfei 5 years ago
parent 9584a97e8d
commit 9190eb8edd
  1. 1
      app/src/main/java/io/legado/app/constant/Action.kt
  2. 39
      app/src/main/java/io/legado/app/service/ReadAloudService.kt
  3. 2
      app/src/main/java/io/legado/app/service/notification/ReadAloudNotification.kt

@ -6,6 +6,7 @@ object Action {
const val stop = "stop" const val stop = "stop"
const val resume = "resume" const val resume = "resume"
const val pause = "pause" const val pause = "pause"
const val addTimer = "addTimer"
const val setTimer = "setTimer" const val setTimer = "setTimer"
const val prevParagraph = "prevParagraph" const val prevParagraph = "prevParagraph"
const val nextParagraph = "nextParagraph" const val nextParagraph = "nextParagraph"

@ -5,6 +5,7 @@ import android.content.*
import android.media.AudioFocusRequest import android.media.AudioFocusRequest
import android.media.AudioManager import android.media.AudioManager
import android.os.Build import android.os.Build
import android.os.Handler
import android.speech.tts.TextToSpeech import android.speech.tts.TextToSpeech
import android.speech.tts.UtteranceProgressListener import android.speech.tts.UtteranceProgressListener
import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.MediaSessionCompat
@ -107,6 +108,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
} }
} }
private val handler = Handler()
private var ttsIsSuccess: Boolean = false private var ttsIsSuccess: Boolean = false
private lateinit var audioManager: AudioManager private lateinit var audioManager: AudioManager
private lateinit var mFocusRequest: AudioFocusRequest private lateinit var mFocusRequest: AudioFocusRequest
@ -118,6 +120,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
private var textChapter: TextChapter? = null private var textChapter: TextChapter? = null
private var pageIndex = 0 private var pageIndex = 0
var mediaSessionCompat: MediaSessionCompat? = null var mediaSessionCompat: MediaSessionCompat? = null
private val dsRunnable: Runnable? = Runnable { doDs() }
var pause = false var pause = false
var title: String = "" var title: String = ""
var subtitle: String = "" var subtitle: String = ""
@ -163,6 +166,8 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
Action.upTtsSpeechRate -> upSpeechRate(true) Action.upTtsSpeechRate -> upSpeechRate(true)
Action.prevParagraph -> prevP() Action.prevParagraph -> prevP()
Action.nextParagraph -> nextP() Action.nextParagraph -> nextP()
Action.addTimer -> addTimer()
Action.setTimer -> setTimer(intent.getIntExtra("minute", 0))
else -> stopSelf() else -> stopSelf()
} }
} }
@ -219,6 +224,40 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
} }
} }
private fun setTimer(minute: Int) {
timeMinute = minute
if (minute > 0) {
handler.removeCallbacks(dsRunnable)
handler.postDelayed(dsRunnable, 6000)
}
ReadAloudNotification.upNotification(this)
}
private fun addTimer() {
if (timeMinute == 60) {
timeMinute = 0
handler.removeCallbacks(dsRunnable)
} else {
timeMinute += 10
if (timeMinute > 60) timeMinute = 60
handler.removeCallbacks(dsRunnable)
handler.postDelayed(dsRunnable, 6000)
}
ReadAloudNotification.upNotification(this)
}
private fun doDs() {
if (!pause) {
timeMinute--
if (timeMinute == 0) {
stopSelf()
} else if (timeMinute > 0) {
handler.postDelayed(dsRunnable, 6000)
}
}
ReadAloudNotification.upNotification(this)
}
private fun upSpeechRate(reset: Boolean = false) { private fun upSpeechRate(reset: Boolean = false) {
if (this.getPrefBoolean("ttsFollowSys", true)) { if (this.getPrefBoolean("ttsFollowSys", true)) {
if (reset) { if (reset) {

@ -51,7 +51,7 @@ object ReadAloudNotification {
builder.addAction( builder.addAction(
R.drawable.ic_time_add_24dp, R.drawable.ic_time_add_24dp,
service.getString(R.string.set_timer), service.getString(R.string.set_timer),
PendingIntentHelp.aloudServicePendingIntent(service, Action.setTimer) PendingIntentHelp.aloudServicePendingIntent(service, Action.addTimer)
) )
builder.setStyle( builder.setStyle(
androidx.media.app.NotificationCompat.MediaStyle() androidx.media.app.NotificationCompat.MediaStyle()

Loading…
Cancel
Save