i am currently working on a personal project for my school and i want the user to press a play button and listen to the alphabet song but it isn't working on the simulator or on a device
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer:AVAudioPlayer?
let backgroundImageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
setBackground()
// Do any additional setup after loading the view.
guard let path = Bundle.main.path(forResource: "ABCSongENG", ofType: "mp3")
else {
return
}
let url = URL(fileURLWithPath: path)
audioPlayer = try? AVAudioPlayer(contentsOf: url, fileTypeHint: nil)
audioPlayer?.prepareToPlay()
}
func setBackground() {
view.addSubview(backgroundImageView)
backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
backgroundImageView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
backgroundImageView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
backgroundImageView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
backgroundImageView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
backgroundImageView.image = UIImage(named: "farm-image")
view.sendSubviewToBack(backgroundImageView)
}
@IBAction func alphabetPlay(_ sender: UIButton) {
audioPlayer?.prepareToPlay()
audioPlayer?.play()
print("playing ABC music")
}
}
is there something wrong with the code or with the device
thank you in advance