Reputation: 119
I wrote the following code that plays a sound when a button is pressed, it works perfectly on the simulator (IPhone7), but not when running on my IPhone X. The mute button is not on on my phone.
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate{
var soundPlayer : AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func notePressed(_ sender: UIButton) {
playNote(noteIndex: sender.tag)
}
func playNote(noteIndex : Int){
let soundURL = Bundle.main.url(forResource: "note\(noteIndex)", withExtension: "wav")
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options:[])
try AVAudioSession.sharedInstance().setActive(true)
soundPlayer = try AVAudioPlayer(contentsOf:soundURL!)
} catch {
print (error)
}
soundPlayer.play()
}
}
Upvotes: 2
Views: 65
Reputation: 119
I finally found out the solution:
Go to: File->Project Settings... , change "Derived Data:" to "Project-relative Location", click "Done".
That worked for me.
Upvotes: 1