Created
November 5, 2019 19:19
-
-
Save nfarah86/7a2cc5172797f4e6c9ce9fafa90570b6 to your computer and use it in GitHub Desktop.
Play a song via AVAudioPlayer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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