MPMoviePlayerController不工作 - Swift

时间:2015-06-04 19:13:39

标签: ios swift mpmovieplayercontroller mpmovieplayer

好的,我有一个视图控制器,我想在viewDidLoad上的MPMoviePlayerController中加载一个视频文件。我使用以下代码,当它运行时,我什么都没有。我使用的代码如下:

import UIKit
import iAd
import MediaPlayer


class VideoPlayer: UIViewController {

    var moviePlayer : MPMoviePlayerController?


    func playVideo() {
        let url = NSURL (string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")
        moviePlayer = MPMoviePlayerController(contentURL: url)
        if let player = moviePlayer {
            player.view.frame = self.view.bounds
            player.prepareToPlay()
            player.scalingMode = .AspectFill
            self.view.addSubview(player.view)
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()

playVideo()
    }
}

从mainViewController到VideoPlayerViewController有一个segue,当按下播放按钮时会触发该segue。我知道文件已正确链接,因为我在文件中设置了iAd。

我将附上以下几张截图: What I get when the simulator runs The Debug Console

1 个答案:

答案 0 :(得分:1)

您提供的代码中没有您的问题。它在你项目的其他地方。 我刚刚创建了一个新的singleView Project粘贴你的代码。把playVideo放在viewDidLoad中。运行它,它会在应用程序启动时显示视频。

import UIKit
import MediaPlayer

class ViewController: UIViewController {

var moviePlayer : MPMoviePlayerController?

func playVideo() {
    let url = NSURL (string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")
    moviePlayer = MPMoviePlayerController(contentURL: url)
    if let player = moviePlayer {
        player.view.frame = self.view.bounds
        player.prepareToPlay()
        player.scalingMode = .AspectFill
        self.view.addSubview(player.view)
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    playVideo()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

添加工作应用的屏幕截图

Screenshot