Is there a way to adjust (reduce) the upper limit of the system volume

Sometimes when I'm putting on or taking off clothes, I accidentally bump the digital crown of my Apple Watch or AirPods Max, and then the volume suddenly becomes very loud, which has been bothering me for a long time.

I followed the instructions in https://support.apple.com/zh-sg/guide/iphone/iphb71f9b54d/ios, but I couldn't find the relevant settings. The system prompt is to "Reduce Loud Audio", rather than to lower the volume (iOS 17.4).

I searched, but I couldn't find any related apps in the App Store. I asked the AI and it provided a relevant solution, so I want to learn Swift and create an app myself (I've only been learning for less than a week). Here's the solution provided by the AI:

  • The general idea is to listen for the routeChange event of AVAudioSession through NotificationCenter
  • then use MPVolumeView to get the slider, and set the value of the slider to control the volume limit.

However, when I debugged it, I found that it didn't work even after setting it. I would like to ask where the problem might be and how I should adjust it?

    @objc func setMaximumVolume () -> Void {
        if !enableMaxvolume {
            return;
        }
        let volumeView = MPVolumeView()
        if let slider = volumeView.subviews.first as? UISlider {
            slider.value = Float(self.maximumVolume / 100)
            print("setMaximumVolume: \(slider.value)")
        }
    }