D
D
Denis2019-04-21 19:20:59
Swift
Denis, 2019-04-21 19:20:59

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)
    }
}

How to make an audio file play only for 10 seconds?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
briahas, 2019-04-26
@foma24

As answered in the comments - set a timer and turn it off

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question