将Button动作添加到MPMoviePlayerController- swift的子视图

时间:2015-11-20 17:58:33

标签: ios iphone swift mpmovieplayercontroller

这里我用来从图库中获取视频并与音频合并,并将最终视频显示到屏幕。我使用MPMoviePlayerController显示合并的视频。在这里,我在MPMoviePlayerController的子视图上添加了一个按钮。当那个按钮点击我的视频应该在新播放器中播放,或者它也可能是一些视图控制器,

需要:

我尝试在屏幕上添加该视频播放器子视图上的按钮。但是无法执行操作方法

 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        button   = UIButton(type: UIButtonType.System) as UIButton
        button.frame = CGRectMake(34, 449, 220, 106)

        button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

       self.view.addSubview(button)
        button.hidden = true

    }

这里的行动:

func buttonAction(sender:NSNotification?){
       print("finishPreload");
    }

这时我的合并视频完成了:

    func exportDidFinish(session: AVAssetExportSession) {
        if session.status == AVAssetExportSessionStatus.Completed {
            let outputURL = session.outputURL
            let library = ALAssetsLibrary()
            if library.videoAtPathIsCompatibleWithSavedPhotosAlbum(outputURL) {
                library.writeVideoAtPathToSavedPhotosAlbum(outputURL,
                    completionBlock: { (assetURL:NSURL!, error:NSError!) -> Void in
                        var title = ""
                        var message = ""
                        if error != nil {
                            title = "Error"
                            message = "Failed to save video"





                        } else {



//                            AVAsset *asset = AVAsset assetWithURL: outputURL

                            self.videoURL = outputURL!

                            self.mediaUI.dismissViewControllerAnimated(true, completion: nil)
                            self.videoPlayer = MPMoviePlayerController()
                            self.videoPlayer.contentURL = self.videoURL


                            self.videoPlayer.controlStyle = .Embedded

                           self.videoPlayer.scalingMode = .AspectFill

                            self.videoPlayer.shouldAutoplay = true


                            self.videoPlayer.view.frame = CGRectMake(34, 449, 220, 106)

                              self.view.addSubview(self.videoPlayer.view)

                            self.videoPlayer.play()
                            self.videoPlayer.prepareToPlay()

                          self.button.hidden = false







                            title = "Success"
                            message = "Video saved"
                        }
                        let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
                        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil))
                        self.presentViewController(alert, animated: true, completion: nil)
                })
            }
        }

      //  activityMonitor.stopAnimating()
        firstAsset = nil
        secondAsset = nil
        audioAsset = nil
    }

我创建此按钮的原因是当我在屏幕上触摸我的视频播放器时,它应该在新播放器或下一个viewcontroller中播放视频。为此我保持按钮动作。但是不能做按钮动作方法。

谢谢!

注意:

如果它不可能。那么有任何替代解决方案 - 当我点击在我的屏幕上显示的合并视频时,它应该在另一个播放器中播放或在另一个新屏幕上播放

0 个答案:

没有答案