"ViewController.Type does not have a member named 'var


I've been working on an App with Swift on the IOS where when you press a button it makes sound (I'm a beginner). This block of code though has been giving me problems, the code works when I just put "The Wilhelm scream sound effect", but when I try to put string it gives me the "ViewController.Type does not have a member named scream" error.

I've been stuck on this for a while, so any help would be appreciated.

class ViewController: UIViewController {

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")

let scream: String = "The Wilhelm scream sound effect"

var pianoSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(scream, ofType: "mp3")!)
var audioPlayer = AVAudioPlayer()


Your code won't work there. You have to put it inside a function as follow:

let audioPlayer = AVAudioPlayer()
let scream = "The Wilhelm scream sound effect"
func playMySound(sound:String) {
    // you can get the file url instead of the path like this:
    let soundUrl = NSBundle.mainBundle().URLForResource(sound, withExtension: "mp3")
    // now you can play your audio using the soundUrl
    // ...        

override func viewDidLoad() {
    // Do any additional setup after loading the view, typically from a nib.
    playMySound("The Wilhelm scream sound effect")
    // or
By : Leo Dabus

This video can help you solving your question :)
By: admin