pull/46/head
kunfei 5 years ago
parent fdcccdafc7
commit b73bb1bfc4
  1. 49
      app/src/main/java/io/legado/app/help/MediaHelp.kt
  2. 45
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  3. 19
      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.os.Build
import android.support.v4.media.session.PlaybackStateCompat
import androidx.annotation.RequiresApi
import io.legado.app.R
object MediaHelp {
@ -33,17 +32,43 @@ object MediaHelp {
or PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE
or PlaybackStateCompat.ACTION_SET_CAPTIONING_ENABLED)
@RequiresApi(Build.VERSION_CODES.O)
fun getFocusRequest(audioFocusChangeListener: AudioManager.OnAudioFocusChangeListener): AudioFocusRequest {
val mPlaybackAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
return AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(mPlaybackAttributes)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(audioFocusChangeListener)
.build()
fun getFocusRequest(audioFocusChangeListener: AudioManager.OnAudioFocusChangeListener): AudioFocusRequest? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val mPlaybackAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(mPlaybackAttributes)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(audioFocusChangeListener)
.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) {

@ -9,7 +9,6 @@ import android.graphics.BitmapFactory
import android.media.AudioFocusRequest
import android.media.AudioManager
import android.media.MediaPlayer
import android.os.Build
import android.os.Handler
import android.support.v4.media.session.MediaSessionCompat
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
class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener {
class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener,
MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener {
companion object {
var isRun = false
@ -37,7 +39,7 @@ class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener
var pause = false
private val handler = Handler()
private lateinit var audioManager: AudioManager
private lateinit var mFocusRequest: AudioFocusRequest
private var mFocusRequest: AudioFocusRequest? = null
private var title: String = ""
private var subtitle: String = ""
private val mediaPlayer = MediaPlayer()
@ -50,9 +52,10 @@ class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener
super.onCreate()
isRun = true
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()
initBroadcastReceiver()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
@ -97,6 +100,19 @@ class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener
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) {
timeMinute = minute
@ -137,23 +153,6 @@ class AudioPlayService : BaseService(), AudioManager.OnAudioFocusChangeListener
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.media.AudioFocusRequest
import android.media.AudioManager
import android.os.Build
import android.os.Handler
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
@ -40,7 +39,7 @@ abstract class BaseReadAloudService : BaseService(),
private val handler = Handler()
private lateinit var audioManager: AudioManager
private lateinit var mFocusRequest: AudioFocusRequest
private var mFocusRequest: AudioFocusRequest? = null
private var broadcastReceiver: BroadcastReceiver? = null
private var mediaSessionCompat: MediaSessionCompat? = null
private var title: String = ""
@ -57,9 +56,7 @@ abstract class BaseReadAloudService : BaseService(),
super.onCreate()
isRun = true
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()
initBroadcastReceiver()
upNotification()
@ -190,17 +187,7 @@ abstract class BaseReadAloudService : BaseService(),
* @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
return MediaHelp.requestFocus(audioManager, this, mFocusRequest)
}
/**

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

Loading…
Cancel
Save