Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPMoviePlayerController shows only black screen - Swift

I have a View inside a ViewController that I want to add a move player controller to it. But so far I'm only getting a black screen. I'm running this on the simulator, and using the apple dev stream which I tested in Safari and it works.

The videoView is added to the controller through an IBOutlet. I'm using Xcode 6 beta 7.

This is all inside a UIViewController.

Declaration of videoView 320x320 (global):

    @IBOutlet var videoView: UIView!

Declaration of MPMoviePlayerController (global):

    var videoPlayer : MPMoviePlayerController = MPMoviePlayerController()

Adding videoPlayer to 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:

Storyboard

Simulator:

enter image description here

Update:

I've also tried doing this. Super simple. And I'm still getting the same result. The frame size is set that way so that I can see that the player has actually been added.

            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()
like image 768
bzmw Avatar asked Sep 30 '22 22:09

bzmw


1 Answers

I ended up ditching MPMoviePlayerController and opted to use AVFoundation instead.

Working example:

Global declarations:

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

inside 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()
like image 189
bzmw Avatar answered Nov 03 '22 00:11

bzmw