ylliX - Online Advertising Network

Reddit – Dive into anything


Hey all,

This is a cross-post with this. I wasn’t sure the etiquette on this so I’m disclaimering.

Perhaps a bit specific of a question, but I’m currently developing an Android app and am looking to recreate the functionality provided by Tasker’s Accessibility Volume action.

So far I have tried the basics of using an Accessibility Service and using audioManager.setStreamVolumeaudioManager.setStreamVolume to set the volume.

This works when the app is in the foreground but not when it is in the background.

I did also try adding some logic to request audio focus and make sure I had DND permissions since I saw that these factor into whether setStreamVolume works. I even tried forcing the accessibility service to also be a foreground service. But no luck.

Tasker’s Accessibility Volume action does exactly what I’m attempting to do, which is change the device volume from the background!

So, I’m curious if anyone has any insight into how to achieve what I’ve described, or maybe just some idea of how Tasker implements this 🙂

Appreciate any help!

Here is the code I ended up with

    if (!notificationManager.isNotificationPolicyAccessGranted) {
    return
    }
    
    val focusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
        .setAudioAttributes(
            AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build()
        )
        .build()
    
    val result = audioManager.requestAudioFocus(focusRequest)
    
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, prevVolume!!, 0)



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *