Skip to content

Instantly share code, notes, and snippets.

@nfarah86
Created November 5, 2019 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfarah86/7a2cc5172797f4e6c9ce9fafa90570b6 to your computer and use it in GitHub Desktop.
Save nfarah86/7a2cc5172797f4e6c9ce9fafa90570b6 to your computer and use it in GitHub Desktop.
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