0

I'm working with iOS using Swift. I want to load a video file using AVPlayer in Swift.
I was able to load an image and a text file from the local server. But I couldn't load a video file. This is the code that I'm using.

let movieUrl = NSURL(string: "http://akjfsjf/ajdfl/ajkf/car.mp4")
let player = AVPlayer(URL: movieUrl!)
let playerController = AVPlayerViewController()

playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame

player.play()
Malitta N
  • 3,384
  • 22
  • 30
shin
  • 11
  • 3

2 Answers2

0

I have tried with following URL and function

func playvideo (){

        let movieUrl = NSURL(string: "http://www.w3schools.com/html/movie.mp4")
        let player = AVPlayer(URL: movieUrl!)
        let playerController = AVPlayerViewController()
        playerController.player = player
        player.play()
        self.addChildViewController(playerController)
        self.view.addSubview(playerController.view)
        playerController.view.frame = self.view.frame
    }

Also from your code i found you are using http rather than https so you need to add following property in plist file.

enter image description here

Abhishek Sharma
  • 3,283
  • 1
  • 13
  • 22
  • thank you for anwser! actually i already added property that you mentioned.. it is not working. when i wrote my url on crome, i can see the video. but when i wrote it on safari.... it was not working... then.. the local server has problem?? i don't know what problem is.... – shin May 13 '16 at 07:07
  • did you tried with `url` which i have added in my `answer`.? – Abhishek Sharma May 13 '16 at 07:35
  • @ Abhishek Sharma yes your url work. but your url is public url. isn't it? my case is private server, my own server url doesn't work. what is different between my url width yours? – shin Mar 30 '17 at 02:27
  • Have you ever found a solution to this? @shin – Hemang Aug 13 '17 at 12:30
0

Try this one,

import UIKit
import AVKit
import AVFoundation


class ViewController: UIViewController {

var playerVC : AVPlayerViewController!
var playerItem : AVPlayerItem!
var player : AVPlayer!
var playerLayer: AVPlayerLayer!

override func viewDidAppear(animated: Bool) {

            let url = NSURL.init(fileURLWithPath: "your file path")

            self.playerItem = AVPlayerItem.init(URL: url!)
            self.player = AVPlayer.init(playerItem: self.playerItem)
            self.playerVC = AVPlayerViewController.init();
            self.playerVC.player = self.player;
            self.player.currentItem!.playbackLikelyToKeepUp

            self.presentViewController(self.playerVC, animated: true) { () -> Void in
                self.playerVC.player?.play()
       }

    }
 }

Hope this helps!

Praveen Kumar
  • 298
  • 2
  • 15
  • did you check that your code work or not? when I compiled your code, it didn't work. plz check your code and modify your code... thank you – shin Mar 30 '17 at 02:50
  • I modified your code and compiled.. but unfortunately it didn't work... even though I use "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4", your code didn't work – shin Mar 30 '17 at 03:27