Reputation: 39
Here is the code causing the error:
import UIKit
import AVFoundation
class ViewController: UIViewController {
var BackgroundAudio = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: Bundle.mainBundle.pathForResources( "Music", ofType: "mp3")!),error: nil)
}
Edit: I am creating a rock paper scissor game with audio here is my code:
import UIKit
import AVFoundation
class ViewController: UIViewController {
let backgroundAudio : AVAudioPlayer = {let url = Bundle.main.url(forResource: "Music", withExtension: "mp3")!
return try! AVAudioPlayer(contentsOf: url)
}()
let controller = ViewController()
controller.backgroundAudio.play()
@IBOutlet weak var cpuImage: UIImageView!
@IBOutlet weak var playerImage: UIImageView!
@IBOutlet weak var cpuScore: UILabel!
@IBOutlet weak var playerScore: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func rockButtonPressed(_ sender: Any) {
playerMove = 1
playerImage.image = UIImage(named: "playerRock.png")
computerChoice(cpuImage: cpuImage)
determineWinner(player: playerScore, cpu: cpuScore)
}
@IBAction func paperButtonPressed(_ sender: Any) {
playerMove = 2
playerImage.image = UIImage(named: "playerPaper.png")
computerChoice(cpuImage: cpuImage)
determineWinner(player: playerScore, cpu: cpuScore)
}
@IBAction func scissorButtonPressed(_ sender: Any) {
playerMove = 3
playerImage.image = UIImage(named: "playerScissor.png")
computerChoice(cpuImage: cpuImage)
determineWinner(player: playerScore, cpu: cpuScore)
}
}
Hope this helps
Upvotes: 0
Views: 2834
Reputation: 285064
You are mixing up Swift 2 and 3 code and even in Swift 2 there is no pathForResources
(plural) API.
If you are looking for an URL use the URL related API:
Bundle.main.url(forResource: "Music", withExtension: "mp3")
and the AVAudioPlayer
initializer throws
do {
let url = Bundle.main.url(forResource: "Music", withExtension: "mp3")!
let backgroundAudio = try AVAudioPlayer(contentsOf: url)
} catch {
print(error)
}
In this case you can even write
let url = Bundle.main.url(forResource: "Music", withExtension: "mp3")!
let backgroundAudio = try! AVAudioPlayer(contentsOf: url)
because the initializer will succeed always (if it does not it's a design error).
Edit:
This is a way to create the audio player with a closure
class ViewController: UIViewController {
let backgroundAudio : AVAudioPlayer = {
let url = Bundle.main.url(forResource: "Music", withExtension: "mp3")!
return try! AVAudioPlayer(contentsOf: url)
}()
}
and play the sound
let controller = ViewController()
controller.backgroundAudio.play()
PS: Do not use NS...
classes like NSURL
if there is a native Swift counterpart (URL
)
Upvotes: 1
Reputation: 1000
The function you are calling in your first line is not named like that anymore. Use this instead:
var BackgroundAudio = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: Bundle.main.path(forResource: "Music", ofType: "mp3")!),error: nil)
Upvotes: 0