0

Please help me to edit my code. I cant seem to fix this error.

import UIKit
import AVKit
import AVFoundation
import MediaPlayer

class ViewController: UIViewController {

    let avPlayerViewControler = AVPlayerViewController()
    var avPlayer:AVPlayer?
    var videoNumber:Int!
    var url:String!

    override func viewDidLoad() {
        super.viewDidLoad()

        let urlpathString:String? = NSBundle.mainBundle().pathForResource("url", ofType: "mp4")

        if let urlPath = urlpathString {

            switch videoNumber {
            case 1 : url="20"
            case 2 : url="10"
            default : break
            }
            let movieUrl = NSURL(fileURLWithPath: urlPath)

        }
        self.avPlayer = AVPlayer(URL: movieUrl)
        self.avPlayerViewControler.player = self.avPlayer

    }

    @IBAction func Cool(sender: UIButton) {
        videoNumber=1
    }
}
7
  • Well, which identifier? Commented Jun 8, 2016 at 17:45
  • Self.avPlayer =AVPlayer(URL: urlPath) Commented Jun 8, 2016 at 17:47
  • 1
    movieUrl is out of scope... Commented Jun 8, 2016 at 17:49
  • What does that mean? How could i fix that? Thanks Commented Jun 8, 2016 at 17:51
  • 1
    You have to learn what "scope" is. :) Commented Jun 8, 2016 at 17:52

1 Answer 1

2

You have to move the two lines regarding the avPlayer and avPlayerViewControler inside the if since the constant movieUrl is only available inside that scope - it only exists inside the scope it was defined in, it does not exist outside of the if-block!

override func viewDidLoad() {
    super.viewDidLoad()

    let urlpathString:String? = NSBundle.mainBundle().pathForResource("url", ofType: "mp4")

    if let urlPath = urlpathString {

        switch videoNumber {
            case 1 : url="20"
            case 2 : url="10"
            default : break
        }
        let movieUrl = NSURL(fileURLWithPath: urlPath)
        self.avPlayer = AVPlayer(URL: movieUrl)
        self.avPlayerViewControler.player = self.avPlayer
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have done what you said. When I click the button it does not play the video. Please help
@MorganEvansMedia unfortunately that is an entirely different issue. You should rethink your code logic, which method does what and gets called at what time. Currently on a button press you only set some number, how should that number change anything?
@MorganEvansMedia ... and you should think about accepting my answer since it apparently fixed your issue, namely the error. Your code still does not run but that is exactly what your second question is for. If you feel like my answer is correct you can click the checkmark on its left.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.