pull/46/head
kunfei 5 years ago
parent fdcccdafc7
commit b73bb1bfc4
  1. 33
      app/src/main/java/io/legado/app/help/MediaHelp.kt
  2. 43
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  3. 17
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  4. 2
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

@ -7,7 +7,6 @@ import android.media.AudioManager
import android.media.MediaPlayer import android.media.MediaPlayer
import android.os.Build import android.os.Build
import android.support.v4.media.session.PlaybackStateCompat import android.support.v4.media.session.PlaybackStateCompat
import androidx.annotation.RequiresApi
import io.legado.app.R import io.legado.app.R
object MediaHelp { object MediaHelp {
@ -33,17 +32,43 @@ object MediaHelp {
or PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE or PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE
or PlaybackStateCompat.ACTION_SET_CAPTIONING_ENABLED) or PlaybackStateCompat.ACTION_SET_CAPTIONING_ENABLED)
@RequiresApi(Build.VERSION_CODES.O) fun getFocusRequest(audioFocusChangeListener: AudioManager.OnAudioFocusChangeListener): AudioFocusRequest? {
fun getFocusRequest(audioFocusChangeListener: AudioManager.OnAudioFocusChangeListener): AudioFocusRequest { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val mPlaybackAttributes = AudioAttributes.Builder() val mPlaybackAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA) .setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build() .build()
return AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(mPlaybackAttributes) .setAudioAttributes(mPlaybackAttributes)
.setAcceptsDelayedFocusGain(true) .setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(audioFocusChangeListener) .setOnAudioFocusChangeListener(audioFocusChangeListener)
.build() .build()
} else {
null
}
}
/**
* @return 音频焦点
*/
fun requestFocus(
audioManager: AudioManager,
listener: AudioManager.OnAudioFocusChangeListener,
focusRequest: AudioFocusRequest?
): Boolean {
val request: Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
focusRequest?.let {
audioManager.requestAudioFocus(focusRequest)
} ?: AudioManager.AUDIOFOCUS_REQUEST_GRANTED
} else {
@Suppress("DEPRECATION")
audioManager.requestAudioFocus(
listener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN
)
}
return request == AudioManager.AUDIOFOCUS_REQUEST_GRANTED
} }
fun playSilentSound(mContext: Context) { fun playSilentSound(mContext: Context) {

@ -9,7 +9,6 @@ import android.graphics.BitmapFactory
import android.media.AudioFocusRequest import android.media.AudioFocusRequest
import android.media.AudioManager import android.media.AudioManager
import android.media.MediaPlayer import android.media.MediaPlayer
import android.os.Build
import android.os.Handler import android.os.Handler
import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat import android.support.v4.media.session.PlaybackStateCompat
@ -27,7 +26,10 @@ import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener { class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener,
MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener {
companion object { companion object {
var isRun = false var isRun = false
@ -37,7 +39,7 @@ class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener
var pause = false var pause = false
private val handler = Handler() private val handler = Handler()
private lateinit var audioManager: AudioManager private lateinit var audioManager: AudioManager
private lateinit var mFocusRequest: AudioFocusRequest private var mFocusRequest: AudioFocusRequest? = null
private var title: String = "" private var title: String = ""
private var subtitle: String = "" private var subtitle: String = ""
private val mediaPlayer = MediaPlayer() private val mediaPlayer = MediaPlayer()
@ -50,9 +52,10 @@ class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener
super.onCreate() super.onCreate()
isRun = true isRun = true
audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mFocusRequest = MediaHelp.getFocusRequest(this) mFocusRequest = MediaHelp.getFocusRequest(this)
} mediaPlayer.setOnErrorListener(this)
mediaPlayer.setOnPreparedListener(this)
mediaPlayer.setOnCompletionListener(this)
initMediaSession() initMediaSession()
initBroadcastReceiver() initBroadcastReceiver()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING) upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
@ -97,6 +100,19 @@ class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener
mediaPlayer.start() mediaPlayer.start()
} }
override fun onPrepared(mp: MediaPlayer?) {
if (pause) return
mp?.start()
}
override fun onError(mp: MediaPlayer?, what: Int, extra: Int): Boolean {
return true
}
override fun onCompletion(mp: MediaPlayer?) {
}
private fun setTimer(minute: Int) { private fun setTimer(minute: Int) {
timeMinute = minute timeMinute = minute
@ -137,23 +153,6 @@ class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener
upNotification() upNotification()
} }
/**
* @return 音频焦点
*/
fun requestFocus(): Boolean {
val request: Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
audioManager.requestAudioFocus(mFocusRequest)
} else {
@Suppress("DEPRECATION")
audioManager.requestAudioFocus(
this,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN
)
}
return request == AudioManager.AUDIOFOCUS_REQUEST_GRANTED
}
/** /**
* 更新媒体状态 * 更新媒体状态
*/ */

@ -8,7 +8,6 @@ import android.content.IntentFilter
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.media.AudioFocusRequest import android.media.AudioFocusRequest
import android.media.AudioManager import android.media.AudioManager
import android.os.Build
import android.os.Handler import android.os.Handler
import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat import android.support.v4.media.session.PlaybackStateCompat
@ -40,7 +39,7 @@ abstract class BaseReadAloudService : BaseService(),
private val handler = Handler() private val handler = Handler()
private lateinit var audioManager: AudioManager private lateinit var audioManager: AudioManager
private lateinit var mFocusRequest: AudioFocusRequest private var mFocusRequest: AudioFocusRequest? = null
private var broadcastReceiver: BroadcastReceiver? = null private var broadcastReceiver: BroadcastReceiver? = null
private var mediaSessionCompat: MediaSessionCompat? = null private var mediaSessionCompat: MediaSessionCompat? = null
private var title: String = "" private var title: String = ""
@ -57,9 +56,7 @@ abstract class BaseReadAloudService : BaseService(),
super.onCreate() super.onCreate()
isRun = true isRun = true
audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mFocusRequest = MediaHelp.getFocusRequest(this) mFocusRequest = MediaHelp.getFocusRequest(this)
}
initMediaSession() initMediaSession()
initBroadcastReceiver() initBroadcastReceiver()
upNotification() upNotification()
@ -190,17 +187,7 @@ abstract class BaseReadAloudService : BaseService(),
* @return 音频焦点 * @return 音频焦点
*/ */
fun requestFocus(): Boolean { fun requestFocus(): Boolean {
val request: Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { return MediaHelp.requestFocus(audioManager, this, mFocusRequest)
audioManager.requestAudioFocus(mFocusRequest)
} else {
@Suppress("DEPRECATION")
audioManager.requestAudioFocus(
this,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN
)
}
return request == AudioManager.AUDIOFOCUS_REQUEST_GRANTED
} }
/** /**

@ -90,7 +90,7 @@ class HttpReadAloudService : BaseReadAloudService(),
@Synchronized @Synchronized
private fun playAudio(fd: FileDescriptor) { private fun playAudio(fd: FileDescriptor) {
if (playingIndex != nowSpeak) { if (playingIndex != nowSpeak && requestFocus()) {
playingIndex = nowSpeak playingIndex = nowSpeak
try { try {
mediaPlayer.reset() mediaPlayer.reset()

Loading…
Cancel
Save