2016-07-04 45 views
1

我有這樣的代碼流從我的服務器視頻:斯威夫特AVPlayer不會出現,只有聲音

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"); 

    } 
} 

的問題是,我什麼都看不到,但能聽到聲音。 問題是什麼?對不起英語不好。

+0

我看不到任何地方你的播放器添加到您視圖。這個問題... http://stackoverflow.com/questions/25932570/how-to-play-video-with-avplayerviewcontroller-avkit-in-swift可能會提供一些幫助。 – Westside

回答

2

試試這個: 先加你的視圖控制器MainViewController這樣,

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

它的工作原理,否則試試這個:

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()