Android ringer plays from phone and Bluetooth headset

619 Views Asked by At

I'm working on an app with a softphone integration so I'm trying to play the phone's default ringtone when an incoming call is detected. The problem I'm having is that when I connect some AirPods to my Pixel 3, the ringtone is played through the phone and the AirPods. The sound coming from the phone is controlled by the Ring volume, while the sound coming through the AirPods is controlled by Media volume. The AirPods are setup for Phone calls and Media audio.

I've tried 2 separate approaches to this but both end up with the same result. First, I tried using a MediaPlayer instance and setting the AudioAttributes to AudioAttributes.USAGE_NOTIFICATION_RINGTONE:

    val uri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE)
    val mp = MediaPlayer().also {
        it.setAudioAttributes(AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
            .build())
        it.setDataSource(this, uri)
        it.prepare()
    }

    findViewById<AppCompatButton>(R.id.start).also { button ->
        button.setOnClickListener {
            mp.isLooping = true
            mp.start()
        }
    }

    findViewById<AppCompatButton>(R.id.stop).also { button ->
        button.setOnClickListener {
            mp.stop()
        }
    }

I've also tried using the RingtoneManager.getRinger() method:

    val uri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE)
    val ringer = RingtoneManager.getRingtone(this, uri)

    findViewById<AppCompatButton>(R.id.start).also { button ->
        button.setOnClickListener {
            ringer.play()
        }
    }

    findViewById<AppCompatButton>(R.id.stop).also { button ->
        button.setOnClickListener {
            ringer.stop()
        }
    }

Both of these approaches yield the same result so I'm not sure where to go from here. I'd like to get it so the ringer is played through the connected Bluetooth device using the ringer volume, similar to how a carrier call comes through.

0

There are 0 best solutions below