티스토리 뷰

There is no programmatic way to control the system volume in iOS

iOS에서는 프로그래밍 방식으로 시스템 음량을 조절 할 수 있는 방법을 제공하지 않지 않는다.

 

but you can use the MediaPlayer framework’s MPVolumeView class to present a standard user interface for controlling system volume.

하지만 "MPVolumeView class"를 사용해서 시스템 볼륨을 제어 할 수 있다.

 

extension MPVolumeView {
    static func setVolume(_ volume: Float) -> Void {
        let volumeView = MPVolumeView()
        let slider = volumeView.subviews.first(where: { $0 is UISlider }) as? UISlider

        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.01) {
            slider?.value = volume
        }
    }
}

struct VolumeSlider: UIViewRepresentable {
   func makeUIView(context: Context) -> MPVolumeView {
       return MPVolumeView(frame: .zero)
   }

   func updateUIView(_ view: MPVolumeView, context: Context) {}
}
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함