Play Sound In Swift 5

[Solved] Play Sound In Swift 5 | Swift - Code Explorer | yomemimo.com
Question : play sound in swift 5

Answered by : gilles-ashley

import AVFoundation
var player: AVAudioPlayer?
func playSound() { guard let path = Bundle.main.path(forResource: "beep", ofType:"mp3") else { return } let url = URL(fileURLWithPath: path) do { player = try AVAudioPlayer(contentsOf: url) player?.play() } catch let error { print(error.localizedDescription) }
}

Source : https://stackoverflow.com/questions/32036146/how-to-play-a-sound-using-swift | Last Update : Fri, 03 Jun 22

Question : playing a sound in swift

Answered by : frantic-falcon-crphvp7510ne

import AVFoundation
var player: AVAudioPlayer?
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) /* The following line is required for the player to work on iOS 11. Change the file type accordingly*/ player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue) /* iOS 10 and earlier require the following line: player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileTypeMPEGLayer3) */ guard let player = player else { return } player.play() } catch let error { print(error.localizedDescription) }
}

Source : https://stackoverflow.com/questions/32036146/how-to-play-a-sound-using-swift | Last Update : Mon, 09 Mar 20

Answers related to play sound in swift 5

Code Explorer Popular Question For Swift