|
|
|
@ -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 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 更新媒体状态 |
|
|
|
|
*/ |
|
|
|
|