I tried disabling every battery saving option and using the dim screen and it didn't work. I also tried everything on a Pixel 3 with stock Android 12 and the behavior was the same.You could either follow steps at dontkillmyapp.com and or use the dim screen action before the shell script action and turn dim off when script finishes.
media dispatch KEY
play, pause, play-pause, mute, headsethook, stop, next, previous, rewind, record, fast-forward.