我正在制作一个在我的服务器上播放视频的视频播放应用程序。我通过以下代码启动了应用程序。
var url:NSURL = NSURL(string: "http://download1.mp4mobilemovies.net/Bollywood/Baahubali%20-%20The%20Beginning%20-%20DvdScr/Baahubali%20-%20The%20Beginning%20-%20DvdScr%20-%201.mp4")!
self.moviePlayer = MPMoviePlayerController()
moviePlayer.movieSourceType = MPMovieSourceType.Streaming
self.moviePlayer = MPMoviePlayerController(contentURL: url)
self.moviePlayer.prepareToPlay()
self.moviePlayer.shouldAutoplay = true
self.moviePlayer.stop()
self.moviePlayer.view.frame = self.view.frame
self.view.addSubview(self.moviePlayer.view)
self.moviePlayer.fullscreen = true
self.moviePlayer.setFullscreen(true, animated: true)
self.moviePlayer.controlStyle = MPMovieControlStyle.Embedded
self.moviePlayer.scalingMode = MPMovieScalingMode.AspectFit
self.moviePlayer.play()
即使我有一个HUD,它在视图出现时开始制作进度指示器,并且我还设置了两个通知对象,以查看视频的回放状态更改以及电影的加载状态。播放状态变化正常。视频开始播放,一段时间后它进入暂停状态,加载状态不会变为STALLED或UNKNOWN,这样我就知道这是一个缓冲状态。请告知缓冲完成后如何处理缓冲状态和自动播放
NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoCanPlay:", name: MPMoviePlayerLoadStateDidChangeNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playbackchanged:", name:MPMoviePlayerPlaybackStateDidChangeNotification, object: nil)
func videoCanPlay(notification: NSNotification){
var moviePlayerController = notification.object
as! MPMoviePlayerController
if(self.moviePlayer?.loadState == MPMovieLoadState.Playable)
{
// let value = UIInterfaceOrientation.LandscapeLeft.rawValue
// UIDevice.currentDevice().setValue(value, forKey: "orientation")
// NSLog("Play");
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
}
///这些状态不会被调用
if(self.moviePlayer?.loadState == MPMovieLoadState.Stalled)
{
NSLog("stalled");
}
if (self.moviePlayer?.loadState == MPMovieLoadState.Unknown)
{
NSLog("unknown");
}
}
答案 0 :(得分:0)
您可以使用此方法检查播放器是否正在缓冲:
self.moviePlayer?.playableDuration
我没有测试过答案,但我认为如果playableDuration为0则播放器当前正在缓冲