Wednesday, 22 May 2019

MediaPlayer - setAudioAttributes not working properly

I'm trying to create an alarm, everything is working fine but the stream type is always media even tho I use STREAM_ALARM, since setStreamType is deprecated, I'm using setAudioAttributes instead but it doesn't seem to work. here's my code :

class AlarmRingtoneManager(val context: Context) {

private lateinit var mediaPlayer: MediaPlayer

fun start() {
    mediaPlayer = MediaPlayer.create(context,  RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM))
        .apply {
        setAudioAttributes(AudioAttributes.Builder()
                .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
                .setLegacyStreamType(AudioManager.STREAM_ALARM)
                .setUsage(AudioAttributes.USAGE_ALARM)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build())
        isLooping = true
        start()
    }
}

fun stop() {
    mediaPlayer.stop()
}
}



from MediaPlayer - setAudioAttributes not working properly

No comments:

Post a Comment