Morgan Evans Media
Morgan Evans Media

Reputation: 123

Video not playing on iPhone

enter image description hereI have just got a simple video going. When I run the app in Simulator the video plays but when I run it on my iPhone it says video missing. Any help would be much appreciated thanks!

Here is my code :

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

    var playerviewcontroller = AVPlayerViewController()
    var playerview = AVPlayer ()

    @IBAction func playMusic(sender: AnyObject) {
        var fileURL = NSURL(fileURLWithPath:"/Users/MorganEvans/Documents/Apps/32134.mp4")
        playerview = AVPlayer(URL: fileURL)

        playerviewcontroller.player = playerview

        self.presentViewController(playerviewcontroller, animated: true){

            self.playerviewcontroller.player?.play()
        }


    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

Upvotes: 1

Views: 177

Answers (2)

Abdul Yasin
Abdul Yasin

Reputation: 3508

Please put the video file into your project bundle. Simply drag and drop the video into your project.

NSURL *url=[[NSBundle mainBundle]URLForResource:@"arreg" withExtension:@"mp4"];
AVPlayer *video=[AVPlayer playerWithURL:url];
AVPlayerViewController *controller=[[AVPlayerViewController alloc]init];
controller.player=video;
[self.view addSubview:controller.view];

controller.view.frame=self.view.frame;
[self addChildViewController:controller];

[video play];

Upvotes: 0

Hossam Ghareeb
Hossam Ghareeb

Reputation: 7113

The video file should be added in Xcode project like this:

enter image description here

Then you can get the video url like this:

let movieURL = NSBundle.mainBundle().URLForResource("ElephantSeals", withExtension: "mov")!

Upvotes: 1

Related Questions