2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【SwiftUI】iPhoneの音量を取得する、コピペで動く

Last updated at Posted at 2023-11-21

始めに

iPhoneのコントロールセンターにある音量を取得するサンプルコードです。
これで、今の音量を取得することができます

サンプルコード

import SwiftUI
import AVFoundation

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }.padding()
            .onAppear {
                // 取得
                let audioSession = AVAudioSession.sharedInstance()
                do {
                    try audioSession.setActive(true)
                    let volume = audioSession.outputVolume
                    print("Output Volume: \(volume)")
                } catch {
                    print("Failed to set audio session active: \(error)")
                }
            }
    }
}

注意

音量を取得しても、Float型で取得することになります

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?