1

Swift 中似乎没有任何 SO 帖子,dismissMoviePlayerViewControllerAnimated所以我想我会开始的。

我有一个表格单元格,当你长按它时,它会显示一个视频。当视频结束时,我的目标是让用户回到表格视图。最后一块是不工作的位。

在这里的任何帮助将不胜感激。我已经阅读了 Apple 文档以及 Objective-C 中关于此的一些帖子。似乎答案是 run dismissMoviePlayerViewControllerAnimated,这是 UIViewController 上的一种方法,但它不起作用。

import UIKit
import MediaPlayer

class ViewController: UIViewController {
    var moviePlayer:MPMoviePlayerController!

    @IBOutlet weak var longPressView: UIView!
    let longPressRec = UILongPressGestureRecognizer()

    func longPressedView() {
        playVideo()
    }

    func videoHasFinishedPlaying(notification: NSNotification){
        println("Video finished playing")

        self.dismissMoviePlayerViewControllerAnimated()
        // not returning me to the ViewController
    }

    func playVideo() {
        // get path and url of movie
        let path = NSBundle.mainBundle().pathForResource("IMG_8602", ofType:"MOV")
        let url = NSURL.fileURLWithPath(path!)
        moviePlayer = MPMoviePlayerController(contentURL: url)

        // construct the views
        moviePlayer.view.frame = self.view.bounds
        self.view.addSubview(moviePlayer.view)
        moviePlayer.fullscreen = true

        // remove controls at top and bottom of video
        moviePlayer.controlStyle = MPMovieControlStyle.None

        // add event observer for videoHasFinsihedPlaying
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoHasFinishedPlaying:",
        name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)
    }

 override func viewDidLoad() {
        super.viewDidLoad()

        longPressRec.addTarget(self, action: "longPressedView")
        longPressView.addGestureRecognizer(longPressRec)
        longPressView.userInteractionEnabled = true

        // Do any additional setup after loading the view, typically from a nib.
    }

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

1 回答 1

1

您的代码不起作用,因为您使用MPMoviePlayerController的是MPMoviePlayerViewController.

您正在调用:

self.dismissMoviePlayerViewControllerAnimated()

但没有MPMoviePlayerViewController解雇。这就是为什么什么都没有发生。

如果您更喜欢使用MPMoviePlayerController(如您发布的代码中),在手动添加它之后view,您还必须手动删除它。

于 2015-01-20T21:53:16.120 回答