import UIKit
import AVFoundation
import AVKit
var player: AVAudioPlayer?
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func play(_ sender: Any) {
guard let url = Bundle.main.url(forResource: "song", withExtension: "wav") else { return }
//PlAY AUDIO with AVAudioPlayer
// do {
// player = try AVAudioPlayer(contentsOf: url)
// guard let player = player else { return }
// player.play()
// } catch let error {
// print(error.localizedDescription)
// }
//PlAY AUDIO with AVPlayer
let player = AVPlayer(url: url)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
present(playerViewController, animated: true) {
player.play()
}
}
@IBAction func stop(_ sender: Any) {
//Stop AUDIO with AVAudioPlayer
// if player != nil {
// player?.stop()
// player = nil
// }
}
}
No comments:
Post a Comment