dismissMoviePlayerViewControllerAnimated 在 Swift 中不工作
dismissMoviePlayerViewControllerAnimated not working in Swift
Swift 中的 dismissMoviePlayerViewControllerAnimated
上似乎没有任何 SO 帖子,所以我想我要开始了。
我有一个 table 单元格,当您长按它时,它会显示一个视频。当视频结束时,我的目标是将用户带回 table 视图。最后一块是不起作用的部分。
如有任何帮助,我们将不胜感激。我已经通读了 Apple 文档和 Objective-C 中关于此的一些帖子。似乎答案是 运行 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.
}
}
您的代码无效,因为您使用的是 MPMoviePlayerController
而不是 MPMoviePlayerViewController
。
您正在呼叫:
self.dismissMoviePlayerViewControllerAnimated()
但没有 MPMoviePlayerViewController
可以忽略。这就是什么都没有发生的原因。
如果您更喜欢使用 MPMoviePlayerController
(如您发布的代码中所示),在手动添加其 view
后,您还必须手动将其删除。
Swift 中的 dismissMoviePlayerViewControllerAnimated
上似乎没有任何 SO 帖子,所以我想我要开始了。
我有一个 table 单元格,当您长按它时,它会显示一个视频。当视频结束时,我的目标是将用户带回 table 视图。最后一块是不起作用的部分。
如有任何帮助,我们将不胜感激。我已经通读了 Apple 文档和 Objective-C 中关于此的一些帖子。似乎答案是 运行 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.
}
}
您的代码无效,因为您使用的是 MPMoviePlayerController
而不是 MPMoviePlayerViewController
。
您正在呼叫:
self.dismissMoviePlayerViewControllerAnimated()
但没有 MPMoviePlayerViewController
可以忽略。这就是什么都没有发生的原因。
如果您更喜欢使用 MPMoviePlayerController
(如您发布的代码中所示),在手动添加其 view
后,您还必须手动将其删除。