MPMoviePlayerController只显示黑屏--Swift

时间:2014-09-10 20:21:36

标签: ios swift mpmovieplayercontroller

我在ViewController中有一个View,我想为它添加一个移动播放器控制器。但到目前为止,我只是一个黑屏。 我在模拟器上运行它,并使用我在Safari中测试的苹果开发流,它可以工作。

视频通过IBOutlet添加到控制器。我正在使用Xcode 6 beta 7。

这都在UIViewController中。

视频宣传320x320(全球):

    @IBOutlet var videoView: UIView!

MPMoviePlayerController声明(全局):

    var videoPlayer : MPMoviePlayerController = MPMoviePlayerController()

将videoPlayer添加到View:

            videoURLWithPath = "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"
            let videoURL = NSURL(fileURLWithPath: videoURLWithPath)
            videoPlayer.movieSourceType = MPMovieSourceType.Streaming;
            videoPlayer.contentURL = videoURL
            videoPlayer.view.frame = videoView.bounds

            videoView.addSubview(videoPlayer.view)
            videoPlayer.controlStyle = MPMovieControlStyle.Embedded

            videoPlayer.prepareToPlay()
            videoPlayer.play()

故事板:

Storyboard

模拟器:

enter image description here

更新

我也尝试过这样做。超级简单。我仍然得到相同的结果。框架大小设置为这样,以便我可以看到玩家实际上已被添加。

            let streamURL = NSURL(string: "http://www.thumbafon.com/code_examples/video/segment_example/prog_index.m3u8")
            var streamPlayer = MPMoviePlayerController(contentURL: streamURL)
            streamPlayer.view.frame = CGRect(x: 10, y: 10, width: 200, height: 200)
            streamPlayer.controlStyle = MPMovieControlStyle.Embedded
            videoView.addSubview(streamPlayer.view)
            streamPlayer.play()

2 个答案:

答案 0 :(得分:4)

我最终放弃了MPMoviePlayerController并选择使用AVFoundation。

工作示例:

全球声明:

var player : AVPlayer? = nil
var playerLayer : AVPlayerLayer? = nil
var asset : AVAsset? = nil
var playerItem: AVPlayerItem? = nil

在viewDidLoad中:

            let videoURLWithPath = "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"
            let videoURL = NSURL(string: videoURLWithPath)

            asset = AVAsset.assetWithURL(videoURL) as? AVAsset
            playerItem = AVPlayerItem(asset: asset)

            player = AVPlayer(playerItem: self.playerItem)

            playerLayer = AVPlayerLayer(player: self.player)
            playerLayer!.frame = videoView.frame
            videoView.layer.addSublayer(self.playerLayer)

            player!.play()

答案 1 :(得分:1)

而不是使用MPMoviePlayerController,这对我来说非常有用:

// import MediaPlayer

var moviePlayer : MPMoviePlayerViewController?

@IBAction func playVideo()  {

    let path = NSBundle.mainBundle().pathForResource("ra3", ofType:"mp4")
    let url = NSURL.fileURLWithPath(path!)

    let videoURL = url
    moviePlayer = MPMoviePlayerViewController(contentURL: videoURL )

    if let player = moviePlayer {
        player.view.frame = self.view.bounds
        self.presentViewController(moviePlayer!, animated: true, completion: nil)
    }
    else {
        NSLog("no player")
    }
}