alex alex - 5 months ago 61
Swift Question

Swift AVPlayer doesn't appear, only sound

i have this code for stream a video from my server :

import AVFoundation
import UIKit
import AVKit

class VideoController : UIViewController{

let avPlayerViewController = AVPlayerViewController()
var avPlayer : AVPlayer?

override func viewDidLoad() {
super.viewDidLoad()
playVideo();
}

func playVideo(){
let movieUrl : NSURL = NSURL(string : "http://---------")!;

self.avPlayer = AVPlayer(URL: movieUrl)
self.avPlayerViewController.player = self.avPlayer

self.avPlayerViewController.player?.play()

print("CI SIAMOOOOOOOOOOOOOOOOOOO");

}
}


the problem is that i can't see anything but able to hear the audio.
What is the problem? sorry for bad english.

Answer

Try this: First add Your View Controller to MainViewController Like this,

 self.presentViewController(self.avPlayerViewController, animated: true, completion: nil);

It Works otherwise try this:

  self.avPlayerViewController.player = AVPlayer(URL: movieUrl)
  self.avPlayerViewController.player?.play()

   or

  var avPlayerItem = AVPlayerItem(URL: movieUrl);
  self.avPlayer = AVPlayer(Item:avPlayerItem);
  self.avPlayerViewController.player?.play()
Comments