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