ios - MPMoviePlayerController 切换视频

标签 ios swift video

我正在播放视频:

let path = NSBundle.mainBundle().pathForResource("Video", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
    player.view.frame = self.view.bounds
    player.prepareToPlay()
    player.scalingMode = .AspectFill
    player.controlStyle = .None
    self.view.addSubview(player.view)
}

我想在按下按钮时将其替换为另一个视频:

@IBAction func aButtonIsPressed(sender: AnyObject) {
    let path = NSBundle.mainBundle().pathForResource("Another Video", ofType:"mp4")
    let url = NSURL.fileURLWithPath(path!)
    moviePlayer = MPMoviePlayerController(contentURL: url)
    if let player = moviePlayer {
        player.view.frame = self.view.bounds
        player.prepareToPlay()
        player.scalingMode = .AspectFill
        player.controlStyle = .None
        self.view.addSubview(player.view)
    }
}

当我点击按钮时,视频会播放,但添加新视频似乎有延迟,这导致过渡不稳定。有什么办法可以最大程度地减少这种延迟吗?我可以尝试添加另一个 MPMoviePlayerController 来预加载第二个视频,但通过一些测试,我发现导致剪辑的行就在这里。

self.view.addSubview(player.view)

我可以一直播放第一个视频,直到出现“didFinish”上方的行吗?那太好了。

最佳答案

您正在创建新的 MPMoviePlayerController 对象,这就是您遇到此问题的原因,您应该使用相同的对象并且该对象已经在播放对象,只需将 contentURL 属性设置为 new资源的URL

If you set this property while a movie is playing, that movie pauses and the new movie begins loading. The new movie starts playing at the beginning

有关更多详细信息,请访问此 link

关于ios - MPMoviePlayerController 切换视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32035007/

相关文章:

objective-c - 边框模糊的 UIView

ios - 不初始化引用枚举类型对象

android - 从Json Service获取视频的缩略图

audio - FFMPEG - 无法使用 av_write_frame() 生成 aac 音频文件

ios - 在 Swift 中从 CoreData 中获取数据数组

ios - Apple 真实支付的应用内购买测试

ios - 如何一次将更改应用于多个 View Controller ?

ios - Imageview 图像未正确设置或过滤

ios - 根据方向更新自动布局约束

jquery - 难以将模态窗口代码集成到div中