问题描述
我有一个视频,我正在尝试使用MPMoviePlayerController播放它加载正常,但在5秒后切断。我找到了这篇文章,但它并不适用于swift。
I have a video I'm trying to play using MPMoviePlayerController and it loads fine, but cuts out after 5 seconds. I found this post, but it isn't really applicable for swift.
这是我的代码。
import MediaPlayer
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var moviePlayer: MPMoviePlayerController?
let url = NSURL(string: "http://path/to/video.m3u8")
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
player.view.sizeToFit()
player.scalingMode = MPMovieScalingMode.None
player.movieSourceType = MPMovieSourceType.Streaming
//player.repeatMode = MPMovieRepeatMode.One
player.play()
self.view.addSubview(player.view)
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "metadataUpdated",
name: MPMoviePlayerTimedMetadataUpdatedNotification,
object: nil)
}
}
推荐答案
你的可以吗? ePlayer
超出范围?您是否尝试将其设为成员变量?
Could your moviePlayer
be going out of scope? Have you tried making it a member variable?
moviePlayer
是的局部变量viewDidLoad
,所以一旦该功能完成,我看不出你的玩家不会被解除分配的任何理由。
moviePlayer
is a local variable of viewDidLoad
, so once that function finishes, I don't see any reason why your player would not be deallocated.
如果你改为使用它该类的变量,其生命周期将延长以匹配您班级的生命周期。
If you instead make it a variable of the class, its lifetime will be extended to match your class's lifetime.
类似
class ViewController: UIViewController {
var player: MPMoviePlayerController?
override func viewDidLoad() {
// ...
self.player = MPMoviePlayerController(contentURL: url) // won't go out of scope at end of viewDidLoad()
// ...
}
这篇关于MPMoviePlayerController在5秒后停止播放 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!