Reputation: 11
1these are the wav files I used
2I attached a screenshot of where it is throwing this error.
this is what I have right now. I thought I had everything correct. I know it has something to do with using an optional but not sure how to fix it.
import UIKit
import AVFoundation
class SecondViewController: UIViewController
{
var audioPlayer: AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
do
{
let catSound = Bundle.main.path(forResource: "Cat", ofType: "wav")
let horseSound = Bundle.main.path(forResource: "Horse", ofType: "wav")
let dogSound = Bundle.main.path(forResource: "Dog", ofType: "wav")
let raccoonSound = Bundle.main.path(forResource: "Raccoon", ofType: "wav")
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: catSound!))
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: horseSound!))
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: dogSound!))
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: raccoonSound!))
}
catch
{
print(error)///
}
}
@IBAction func cat(_ sender: Any)
{
audioPlayer?.play()
}
@IBAction func horse(_ sender: Any)
{
audioPlayer?.play()
}
@IBAction func dog(_ sender: Any)
{
audioPlayer?.play()
}
@IBAction func raccoon(_ sender: Any){
audioPlayer?.play()
}
}
Upvotes: 0
Views: 326
Reputation: 285082
Case sensitivity matters.
The files are lowercased ("cat") but the parameter strings in the code are uppercased ("Cat"). The parameter string and the file name must match exactly.
Your code is not very useful anyway because it overwrites the audio player instance three times in viewDidLoad
.
A more efficient solution is to create a method to load the file and play the sound and to use the URL related API of Bundle
.
The force unwrapping is fine because all files must be in the bundle at compile time. A design mistake (file is missing or name is misspellt) can be fixed immediately.
class SecondViewController: UIViewController
{
var audioPlayer: AVAudioPlayer!
@IBAction func cat(_ sender: Any) {
playSound(withName: "cat")
}
@IBAction func horse(_ sender: Any) {
playSound(withName: "horse")
}
@IBAction func dog(_ sender: Any) {
playSound(withName: "dog")
}
@IBAction func raccoon(_ sender: Any){
playSound(withName: "raccoon")
}
func playSound(withName name : String) {
let sound = Bundle.main.url(forResource: name, withExtension: "wav")!
audioPlayer = try! AVAudioPlayer(contentsOf: sound)
audioPlayer.play()
}
}
Upvotes: 0
Reputation: 375
As @Chris and @inexcitus mentioned, your complete code would look like.
class SecondViewController: UIViewController {
var audioPlayer: AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func cat(_ sender: Any)
{
if let catSound = Bundle.main.path(forResource: "cat", ofType: "wav") {
audioPlayer = try? AVAudioPlayer(contentsOf: URL(fileURLWithPath: catSound))
audioPlayer?.play()
}else {
print("Cat File is missing")
}
}
@IBAction func horse(_ sender: Any)
{
if let horseSound = Bundle.main.path(forResource: "horse", ofType: "wav") {
audioPlayer = try? AVAudioPlayer(contentsOf: URL(fileURLWithPath: horseSound))
audioPlayer?.play()
}else {
print("Horse File is missing")
}
}
@IBAction func dog(_ sender: Any)
{
if let dogSound = Bundle.main.path(forResource: "dog", ofType: "wav") {
audioPlayer = try? AVAudioPlayer(contentsOf: URL(fileURLWithPath: dogSound))
audioPlayer?.play()
}else {
print("Dog File is missing")
}
}
@IBAction func raccoon(_ sender: Any)
{
if let raccoonSound = Bundle.main.path(forResource: "raccoon", ofType: "wav") {
audioPlayer = try? AVAudioPlayer(contentsOf: URL(fileURLWithPath: raccoonSound))
audioPlayer?.play()
}else {
print("Raccoon File is missing")
}
}}
Upvotes: 1
Reputation: 2639
One of your resources is nil. You should check if your resoruce is nil before (forcefully) unwrapping it:
if let catSound = Bundle.main.path(forResource: "Cat", ofType: "wav")
{
audioPlayer = try? AVAudioPlayer(contentsOf: URL(fileURLWithPath: catSound))
}
Do this for every resource and use the debugger to see which one is nil.
Upvotes: 0