来自 MPMoviePlayerController 的音频在转至下一个视图控制器后继续播放
Audio from MPMoviePlayerController continues playing after segue to next view controller
添加视频文件后,即使按下一个 VC 的转接键,视频预览中的音频仍会继续播放。
有什么办法可以在按下segue后停止播放音频吗?
var objMoviePlayerController: MPMoviePlayerController = MPMoviePlayerController()
var urlVideo: NSURL = NSURL()
@IBOutlet weak var videoprofileView: UIImageView!
@IBOutlet weak var addvideoBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
var theWidth = view.frame.size.width
var theHeight = view.frame.size.height
videoprofileView.frame = CGRectMake(0, 50, theWidth, theWidth)
addvideoBtn.center = CGPointMake(theWidth/2, self.videoprofileView.frame.maxY+50)
}
@IBAction func addvideoBtn_click(sender: AnyObject) {
var ipcVideo = UIImagePickerController()
ipcVideo.delegate = self
ipcVideo.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
ipcVideo.allowsEditing = true
ipcVideo.videoMaximumDuration = 15
var kUTTypeMovieAnyObject : AnyObject = kUTTypeMovie as AnyObject
ipcVideo.mediaTypes = [kUTTypeMovieAnyObject]
self.presentViewController(ipcVideo, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
urlVideo = (info as NSDictionary).objectForKey(UIImagePickerControllerMediaURL) as! NSURL
self.dismissViewControllerAnimated(true, completion: nil)
objMoviePlayerController = MPMoviePlayerController(contentURL: urlVideo)
objMoviePlayerController.movieSourceType = MPMovieSourceType.Unknown
objMoviePlayerController.view.frame = self.videoprofileView.bounds
objMoviePlayerController.scalingMode = MPMovieScalingMode.AspectFill
objMoviePlayerController.controlStyle = MPMovieControlStyle.None
objMoviePlayerController.shouldAutoplay = true
videoprofileView.addSubview(objMoviePlayerController.view)
objMoviePlayerController.prepareToPlay()
objMoviePlayerController.play()
}
@IBAction func next_click(sender: AnyObject) {
let data:NSData = NSData(contentsOfURL: urlVideo)!
let file = PFFile(name:"video.mp4", data:data)
var currentUser = PFUser.currentUser()!
currentUser["video"] = file
currentUser.saveInBackgroundWithBlock( {
(succeeded: Bool, error: NSError?) -> Void in
if error == nil {
println("video saved")
} else {
println("couldn't save video")
}
})
}
在转到下一个视图控制器之前,将您的视频告诉 stop
。
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
objMoviePlayerController.stop()
}
添加视频文件后,即使按下一个 VC 的转接键,视频预览中的音频仍会继续播放。
有什么办法可以在按下segue后停止播放音频吗?
var objMoviePlayerController: MPMoviePlayerController = MPMoviePlayerController()
var urlVideo: NSURL = NSURL()
@IBOutlet weak var videoprofileView: UIImageView!
@IBOutlet weak var addvideoBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
var theWidth = view.frame.size.width
var theHeight = view.frame.size.height
videoprofileView.frame = CGRectMake(0, 50, theWidth, theWidth)
addvideoBtn.center = CGPointMake(theWidth/2, self.videoprofileView.frame.maxY+50)
}
@IBAction func addvideoBtn_click(sender: AnyObject) {
var ipcVideo = UIImagePickerController()
ipcVideo.delegate = self
ipcVideo.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
ipcVideo.allowsEditing = true
ipcVideo.videoMaximumDuration = 15
var kUTTypeMovieAnyObject : AnyObject = kUTTypeMovie as AnyObject
ipcVideo.mediaTypes = [kUTTypeMovieAnyObject]
self.presentViewController(ipcVideo, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
urlVideo = (info as NSDictionary).objectForKey(UIImagePickerControllerMediaURL) as! NSURL
self.dismissViewControllerAnimated(true, completion: nil)
objMoviePlayerController = MPMoviePlayerController(contentURL: urlVideo)
objMoviePlayerController.movieSourceType = MPMovieSourceType.Unknown
objMoviePlayerController.view.frame = self.videoprofileView.bounds
objMoviePlayerController.scalingMode = MPMovieScalingMode.AspectFill
objMoviePlayerController.controlStyle = MPMovieControlStyle.None
objMoviePlayerController.shouldAutoplay = true
videoprofileView.addSubview(objMoviePlayerController.view)
objMoviePlayerController.prepareToPlay()
objMoviePlayerController.play()
}
@IBAction func next_click(sender: AnyObject) {
let data:NSData = NSData(contentsOfURL: urlVideo)!
let file = PFFile(name:"video.mp4", data:data)
var currentUser = PFUser.currentUser()!
currentUser["video"] = file
currentUser.saveInBackgroundWithBlock( {
(succeeded: Bool, error: NSError?) -> Void in
if error == nil {
println("video saved")
} else {
println("couldn't save video")
}
})
}
在转到下一个视图控制器之前,将您的视频告诉 stop
。
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
objMoviePlayerController.stop()
}