MPMoviePlayerController在play() - swift中被取消分配

时间:2015-07-21 17:06:28

标签: ios swift

我正在尝试向视图控制器添加MPMoviePlayerController,但它正在play()调用时被取消分配。该URL在打印时有效,我也在不同的viewController中使用几乎完全相同的代码片段,它完全正常。视图控制器之间的唯一区别是这个视图控制器是从另一个viewController进行的。我一直在绞尽脑汁,尝试了我能在这里找到的所有类似的解决方案,所以任何帮助都会非常感激。

准备争论

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ShowChannel" {
        if let destination = segue.destinationViewController as? ShowChannelController {
            destination.channelId = channelId
            destination.postsCollection = postsCollection
        }
    }
}

MPMoviePlayerController代码

var newMoviePlayer: MPMoviePlayerController!
var postsCollection = [Post]()
var videoPosition = 0
var channelId: String = ""

override func viewDidLoad() {
    super.viewDidLoad()

    startPlayingVideo(videoPosition)
    updateNowPlaying(videoPosition)

} 

func videoHasFinishedPlaying(notification: NSNotification) {
    /* Find out what the reason was for the player to stop */
    let reason =
    notification.userInfo![MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]
        as! NSNumber?
    if let theReason = reason{

        let reasonValue = MPMovieFinishReason(rawValue: theReason.integerValue)

        stopPlayingVideo()

        switch reasonValue!{
        case .PlaybackEnded:
            if (videoPosition < self.postsCollection.count - 1) {
                videoPosition++
            } else {
                videoPosition = 0
            }
            startPlayingVideo(videoPosition)
            let rowToSelect:NSIndexPath = NSIndexPath(forRow: videoPosition, inSection: 0);
            self.tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None);
            updateNowPlaying(videoPosition)

        case .PlaybackError:
            /* An error happened and the movie ended */
            print("Error happened")
        case .UserExited:
            /* The user exited the player */
            print("User exited")
        }

    }

}

/** What to do when we stop playing a video */
func stopPlayingVideo() {
    if let player = self.newMoviePlayer {
        NSNotificationCenter.defaultCenter().removeObserver(self)
        player.stop()
        player.view.removeFromSuperview()
    }

}

/** Initializes and starts the video player */
func startPlayingVideo(index: Int) {
    let url: NSURL = NSURL(string: postsCollection[index].url)!
    print(postsCollection[index].url)
    self.newMoviePlayer = MPMoviePlayerController(contentURL: url)

    if let player = self.newMoviePlayer {

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "videoHasFinishedPlaying:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)

        player.view.frame = CGRect(x: 0, y: ((self.view.bounds.size.height * 0.45) - (self.view.bounds.size.height / 3)), width: self.view.bounds.size.width, height: self.view.bounds.size.height / 3)

        self.view.addSubview(player.view)
        player.prepareToPlay()
        player.play()
        player.controlStyle = MPMovieControlStyle.Embedded

    } else {
        print("Failed to instantiate the movie player")
    }

}

错误:

  

***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [OS_xpc_dictionary _postNotificationName:object:userInfo:]:无法识别的选择器发送到实例0x17ed2240'

Zombie已启用:

  

- [MPMoviePlayerControllerNew _postNotificationName:object:userInfo:]:发送到解除分配的实例0x17f47400的消息**

1 个答案:

答案 0 :(得分:0)

终于实现了我的错误:

第一个ViewController提到有类似的实现,并且工作正常,没有正确处理&#39; NotificationCenter&#39;而且两个单独课程的观察者都感到困惑。

<强>解决方案:

i.e count = 2 vs count = models.IntegerField(default=2) ViewController

中删除观察者

<强> FirstViewController.swift

viewDidDisappear()

获得的经验:不要忘记注册自己!