SwiftUI「Volume」の音量調整をMPVolumeView化

SwiftUI版開発時のブログ

UIViewRepresentableでSwiftUI対応

import MediaPlayer
import SwiftUI

struct UIKitMPVolumeView: UIViewRepresentable {
    typealias UIViewType = MPVolumeView
    
    let view: MPVolumeView = MPVolumeView()
    
    func makeUIView(context: Context) -> MPVolumeView {
        view.setVolumeThumbImage(UIImage(named: "volume"), for: .normal)
        return view
    }
   
    func updateUIView(_ uiView: MPVolumeView, context: Context) {
    }
    
    func getVolume() -> Float {
        var volume: Float = 0.0
        for subview in view.subviews {
            if let uislider = subview as? UISlider {
                print(uislider.value)
                volume = uislider.value
            }
        }
        return volume
    }
}

Git Hub

https://github.com/cloudsquare22/VolumeSwiftUI

現在版の画面キャプチャ

Swift

Posted by shi-n