// // ContentView.swift // testAudioPlayer // // Created by κΉ€μˆ˜ν™˜ on 6/7/24. // import SwiftUI import AVFoundation struct ContentView: View { @State var duration: Double = 0 var body: some View { VStack { Button { guard let audioURL = Bundle.main.url( forResource: "example", withExtension: "mp3" ) else { return } guard let audioplayer = try? AVAudioPlayer(contentsOf: audioURL) else { return } duration = audioplayer.duration } label: { Text("press to check example1's duration") } .padding(.bottom, 30) Button { guard let audioURL = Bundle.main.url( forResource: "example2", withExtension: "mp3" ) else { return } guard let audioplayer = try? AVAudioPlayer(contentsOf: audioURL) else { return } duration = audioplayer.duration } label: { Text("press to check example2's duration") } .padding(.bottom, 30) Text("duration: \(duration)") } .padding() } }