0

I'm trying to play an mp4 video from documents path in a AVPlayerViewController with an AVPlayer but it doesn't appear anything on screen.What am I doing wrong?

Here a screenshot of the simulator: http://postimg.org/image/xma60nfrf/

and this is my code:

func startPlayer(filePath:String)
{
    let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask, true)
    let docsDir = dirPaths[0]


    let filePath = (docsDir as NSString).stringByAppendingPathComponent(videos[videoNo]._Name)

    playerVC=AVPlayerViewController()
    playerVC!.view.frame=self.view.bounds
    playerVC!.view.layer.zPosition = 1;


    var videoURL = NSURL(string: filePath)
    var player = AVPlayer(URL: videoURL!)
    var playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = playerVC!.view.bounds
    playerVC!.view.layer.addSublayer(playerLayer)

    playerVC!.player=player
    self.view.addSubview(playerVC!.view)

    playerVC!.player!.play()
}
Nikunj
  • 655
  • 3
  • 13
  • 25
solero_ice
  • 49
  • 1
  • 6

2 Answers2

0

Use AVAsset as source for player.

let currentVideoAsset = AVAsset(URL: outputFileURL)


self.player = AVPlayer(playerItem: AVPlayerItem(asset:currentVideoAsset))
Saqib Omer
  • 5,387
  • 7
  • 50
  • 71
0

I found it,I replaced this

var videoURL = NSURL(string: filePath)

with this

var videoURL = NSURL(fileURLWithPath: filePath)

and is working..

solero_ice
  • 49
  • 1
  • 6