Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Play a song via AVAudioPlayer
import UIKit
import AVFoundation
var audioPlayer: AVAudioPlayer?
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
getSoundFile()
}
private func getSoundFile() {
let sound = Bundle.main.path(forResource: "your file's name", ofType: "your file's extension")
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: sound!))
audioPlayer!.play()
}
catch {
print("error")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.