mirror of
https://github.com/apognu/otter
synced 2025-02-18 19:50:35 +01:00
Fixed audio focus stealing.
This commit is contained in:
parent
85e9f14e2a
commit
5c684b6e67
@ -14,7 +14,6 @@ import com.github.apognu.otter.R
|
|||||||
import com.github.apognu.otter.activities.MainActivity
|
import com.github.apognu.otter.activities.MainActivity
|
||||||
import com.github.apognu.otter.utils.AppContext
|
import com.github.apognu.otter.utils.AppContext
|
||||||
import com.github.apognu.otter.utils.Track
|
import com.github.apognu.otter.utils.Track
|
||||||
import com.github.apognu.otter.utils.log
|
|
||||||
import com.github.apognu.otter.utils.maybeNormalizeUrl
|
import com.github.apognu.otter.utils.maybeNormalizeUrl
|
||||||
import com.squareup.picasso.Picasso
|
import com.squareup.picasso.Picasso
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
@ -29,8 +28,6 @@ class MediaControlsManager(val context: Service, private val scope: CoroutineSco
|
|||||||
private var notification: Notification? = null
|
private var notification: Notification? = null
|
||||||
|
|
||||||
fun updateNotification(track: Track?, playing: Boolean) {
|
fun updateNotification(track: Track?, playing: Boolean) {
|
||||||
"updateNotification".log()
|
|
||||||
|
|
||||||
if (notification == null && !playing) return
|
if (notification == null && !playing) return
|
||||||
|
|
||||||
track?.let {
|
track?.let {
|
||||||
|
@ -12,6 +12,7 @@ import android.media.MediaMetadata
|
|||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.support.v4.media.MediaMetadataCompat
|
import android.support.v4.media.MediaMetadataCompat
|
||||||
|
import android.view.KeyEvent
|
||||||
import androidx.core.app.NotificationManagerCompat
|
import androidx.core.app.NotificationManagerCompat
|
||||||
import androidx.media.session.MediaButtonReceiver
|
import androidx.media.session.MediaButtonReceiver
|
||||||
import com.github.apognu.otter.Otter
|
import com.github.apognu.otter.Otter
|
||||||
@ -58,7 +59,15 @@ class PlayerService : Service() {
|
|||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
intent?.action?.let {
|
intent?.action?.let {
|
||||||
if (it == Intent.ACTION_MEDIA_BUTTON) {
|
if (it == Intent.ACTION_MEDIA_BUTTON) {
|
||||||
MediaButtonReceiver.handleIntent(Otter.get().mediaSession.session, intent)
|
intent.extras?.getParcelable<KeyEvent>(Intent.EXTRA_KEY_EVENT)?.let { key ->
|
||||||
|
when (key.keyCode) {
|
||||||
|
KeyEvent.KEYCODE_MEDIA_PLAY, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE -> {
|
||||||
|
if (hasAudioFocus(true)) MediaButtonReceiver.handleIntent(Otter.get().mediaSession.session, intent)
|
||||||
|
Unit
|
||||||
|
}
|
||||||
|
else -> MediaButtonReceiver.handleIntent(Otter.get().mediaSession.session, intent)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,6 +80,7 @@ class PlayerService : Service() {
|
|||||||
return START_STICKY
|
return START_STICKY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("NewApi")
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
|
|
||||||
@ -79,7 +89,7 @@ class PlayerService : Service() {
|
|||||||
|
|
||||||
audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
Build.VERSION_CODES.O.onApi {
|
||||||
audioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN).run {
|
audioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN).run {
|
||||||
setAudioAttributes(AudioAttributes.Builder().run {
|
setAudioAttributes(AudioAttributes.Builder().run {
|
||||||
setUsage(AudioAttributes.USAGE_MEDIA)
|
setUsage(AudioAttributes.USAGE_MEDIA)
|
||||||
@ -253,7 +263,6 @@ class PlayerService : Service() {
|
|||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("NewApi")
|
|
||||||
private fun setPlaybackState(state: Boolean) {
|
private fun setPlaybackState(state: Boolean) {
|
||||||
if (!state) {
|
if (!state) {
|
||||||
val (progress, _, _) = getProgress()
|
val (progress, _, _) = getProgress()
|
||||||
@ -265,32 +274,7 @@ class PlayerService : Service() {
|
|||||||
player.prepare(queue.datasources)
|
player.prepare(queue.datasources)
|
||||||
}
|
}
|
||||||
|
|
||||||
var allowed = !state
|
if (hasAudioFocus(state)) {
|
||||||
|
|
||||||
if (!allowed) {
|
|
||||||
Build.VERSION_CODES.O.onApi(
|
|
||||||
{
|
|
||||||
audioFocusRequest?.let {
|
|
||||||
allowed = when (audioManager.requestAudioFocus(it)) {
|
|
||||||
AudioManager.AUDIOFOCUS_REQUEST_GRANTED -> true
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
|
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
audioManager.requestAudioFocus(audioFocusChangeListener, AudioAttributes.CONTENT_TYPE_MUSIC, AudioManager.AUDIOFOCUS_GAIN).let {
|
|
||||||
allowed = when (it) {
|
|
||||||
AudioManager.AUDIOFOCUS_REQUEST_GRANTED -> true
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (allowed) {
|
|
||||||
player.playWhenReady = state
|
player.playWhenReady = state
|
||||||
|
|
||||||
EventBus.send(Event.StateChanged(state))
|
EventBus.send(Event.StateChanged(state))
|
||||||
@ -358,6 +342,36 @@ class PlayerService : Service() {
|
|||||||
return mediaMetadataBuilder.build()
|
return mediaMetadataBuilder.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("NewApi")
|
||||||
|
private fun hasAudioFocus(state: Boolean): Boolean {
|
||||||
|
var allowed = !state
|
||||||
|
|
||||||
|
if (!allowed) {
|
||||||
|
Build.VERSION_CODES.O.onApi(
|
||||||
|
{
|
||||||
|
audioFocusRequest?.let {
|
||||||
|
allowed = when (audioManager.requestAudioFocus(it)) {
|
||||||
|
AudioManager.AUDIOFOCUS_REQUEST_GRANTED -> true
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
audioManager.requestAudioFocus(audioFocusChangeListener, AudioAttributes.CONTENT_TYPE_MUSIC, AudioManager.AUDIOFOCUS_GAIN).let {
|
||||||
|
allowed = when (it) {
|
||||||
|
AudioManager.AUDIOFOCUS_REQUEST_GRANTED -> true
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return allowed
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
inner class PlayerEventListener : Player.EventListener {
|
inner class PlayerEventListener : Player.EventListener {
|
||||||
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
|
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user