3

I have this code but it doesn't show anything only a black frame.

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    var filepath: NSString = NSBundle.mainBundle().pathForResource("Akbar_ipad", ofType: "mp4")
    var fileURL: NSURL = NSURL(string: filepath)
    let moviePlayerController = MPMoviePlayerController(contentURL: fileURL)

    moviePlayerController.shouldAutoplay = true
    moviePlayerController.movieSourceType = MPMovieSourceType.File


    moviePlayerController.view.frame = CGRect(x: 200, y: 200, width: 500, height: 300)



    self.view.addSubview(moviePlayerController.view)

    moviePlayerController.prepareToPlay()
    moviePlayerController.play()

    ...

Have you got any idea how can i solve my problem? I am going crazy!

Thanks for your time.

cotorusso
  • 125
  • 1
  • 10

2 Answers2

4

I have a similar problem: How to load MPMoviePlayerController contentUrl asynchronous when loading view?

But here some ideas for you:

  1. Maybe the url is constructed incorrectly/not local. Try using: let fileURL = NSBundle.mainBundle().URLForResource("Akbar_ipad", withExtension: "mp4")

  2. You might need a global variable to hold a reference to the movie player instance (as described in the documentation.

Community
  • 1
  • 1
Kreisquadratur
  • 999
  • 8
  • 26
  • 1
    I don't understand why NSBundle.mainBundle().pathForResource didn't work while NSBundle.mainBundle().URLForResource did. Thanks :) – iMemon Oct 31 '14 at 07:56
1

For your problem, you maybe forgot add your resources as "Bundle Resources" Follow below step to add:

  1. In the Project Navigator select your project root
  2. Select "Build Phases" tab
  3. In "Bundle Resources" section to click "+" to add your resources

After that I think you can use both URLForResource and pathForResource

Wilson Wu
  • 1,790
  • 19
  • 13