I'm trying to play a video from a server using Swift.
I have imported MediaPlayer framework, here is my code:
import UIKit
import MediaPlayer
class VideoViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")
var moviePlayer = MPMoviePlayerController(contentURL: url)
moviePlayer.view.frame = CGRect(x: 20, y: 100, width: 200, height: 150)
self.view.addSubview(moviePlayer.view)
moviePlayer.fullscreen = true
moviePlayer.controlStyle = MPMovieControlStyle.Embedded
}
}
I only get a black box when I run in the simulator but no video is playing no matter where I try to load a video from.
UPDATE
Here is the current code
var url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")
var moviePlayer = MPMoviePlayerController(contentURL: url)
moviePlayer.view.frame = CGRect(x: 20, y: 100, width: 200, height: 150)
moviePlayer.movieSourceType = MPMovieSourceType.File
self.view.addSubview(moviePlayer.view)
moviePlayer.prepareToPlay()
moviePlayer.play()
This code interestingly plays ~2 seconds of video before going black again!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…