0

I was following this tutorial on YouTub: https://www.youtube.com/watch?v=qC6DzF_ACpQ&t=714s , and at 12.18 when he clicks the button, it plays audio. I followed all the exact same steps as he did in the video and it is not working for me.

I am pretty new to coding in swift so I did not try anything else other than restarting the whole project again, but it did not work.

import UIKit
import AVFoundation

class ViewController: UIViewController {


    var audioPlayer: AVAudioPlayer?


    override func viewDidLoad() {
        super.viewDidLoad()

    }


    @IBAction func aTapped(_ sender: Any) {


        let url = Bundle.main.url(forResource: "a", withExtension: "mp3")

        // Make sure that weve got the url, otherwise abord
        guard url != nil else{
            return
        }


        do{
            audioPlayer = try AVAudioPlayer(contentsOf: url!)
            audioPlayer?.play()
        }
        catch{
            print("error")
        }


    }

I just want the audio to play when I click the button assigned to the code, but when I click the button, no audio plays at all. Please Help.

Nick
  • 138,499
  • 22
  • 57
  • 95

0 Answers0