2

I try to make music player service when another music player is playing, the player from my app is paused. If another music player is paused or stopped, music from my app is playing. When the focusChange in AudioManager.OnAudioFocusChangeListener() is -1 or you can say it AudioManager.AUDIOFOCUS_LOSS, that I interrupted audio from my music app with audio from another music app, AudioManager.OnAudioFocusChangeListener() is not working anymore and I cannot make audio from my app paused again when another audio from another app running and they are playing audio together.

This is my code

buildNotificationChannel()
registerReceiver(broadcastReceiver, IntentFilter("Tracks"))

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
    this.startForegroundService(Intent(baseContext, StartMusicPlayerService::class.java))
}
else
{
    startService(Intent(baseContext, StartMusicPlayerService::class.java))
}

First, this is a MainActivity class code that leading to the StartMusicPlayerService class that contains AudioManager.OnAudioFocusChangeListener() in onCreate()

override fun onCreate()
{
    super.onCreate()
    val idNotification = (System.currentTimeMillis() % 10000).toInt()
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        startForeground(idNotification, Notification.Builder(this, NotificationService.channelId).build())

        audioManager = getSystemService(AUDIO_SERVICE) as AudioManager
        playbackAttributes = AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_ALARM)
            .setUsage(AudioAttributes.USAGE_GAME)
            .setUsage(AudioAttributes.USAGE_ASSISTANT)
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .setUsage(AudioAttributes.USAGE_UNKNOWN)
            .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
            .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION_SIGNALLING)
            .setUsage(AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE)
            .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .build()

        val mHandler = Handler(Looper.getMainLooper())

        val audioFocusChangeListener =
            AudioManager.OnAudioFocusChangeListener()
            { focusChange ->
                val musicActivity = MusicActivity()

                Log.d("AudioListener", focusChange.toString())

                when (focusChange)
                {
                    AudioManager.AUDIOFOCUS_LOSS, AudioManager.AUDIOFOCUS_GAIN -> //-1 and 1
                    {
                        if (!resting && player != null) //Music is not resting
                        {
                            musicActivity.playThisPlayer()
                        }
                    }

                    AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> //-2
                    {
                        if (!resting && player != null) //Music is not resting
                        {
                            musicActivity.pauseThisPlayer()
                        }
                    }
                }
            }

        val focusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
            .setAudioAttributes(playbackAttributes)
            .setAcceptsDelayedFocusGain(true)
            .setOnAudioFocusChangeListener(audioFocusChangeListener)
            .build()

        audioFocusRequest = audioManager.requestAudioFocus(focusRequest)
    }
}

override fun onDestroy()
{
    super.onDestroy()
}

override fun onTaskRemoved(rootIntent: Intent)
{
    stopSelf()
}

And then this is the StartMusicPlayerService class code that contains focus in AudioManager.OnAudioFocusChangeListener(). I make the AudioFocus in this Service class (only implements Service())

So, what should I do to make AudioManager.OnAudioFocusChangeListener() is working again after the focusChange is -1? I searching the solutions, but it not solve my problem. (I don't know much about Service and AudioFocus)

pup_in_the_tree
  • 169
  • 1
  • 1
  • 8

0 Answers0