Answer the question
In order to leave comments, you need to log in
How to make an audio file only play for 10 seconds in Swift?
Hello.
There is a 30 second audio file. And there is a code that plays it:
func playSound() {
guard let url = Bundle.main.url(forResource: soundName, withExtension: “mp3”) else { return }
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)
guard let player = player else { return }
player.play()
} catch let error {
print(error.localizedDescription)
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question