Skip to content

Instantly share code, notes, and snippets.

@burhanaksendir
Created September 11, 2015 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burhanaksendir/585be8bd6932151c2a44 to your computer and use it in GitHub Desktop.
Save burhanaksendir/585be8bd6932151c2a44 to your computer and use it in GitHub Desktop.
How to take a screenshot from an video playing through MPMediaPlayerController
import UIKit
import MediaPlayer
class ViewController: UIViewController {
var moviePlayer: MPMoviePlayerController?
var playButton: UIButton!
func videoHasFinishedPlaying(notification: NSNotification){
print("Video finished playing")
let reason =
notification.userInfo![MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]
as! NSNumber?
if let theReason = reason{
let reasonValue = MPMovieFinishReason(rawValue: theReason.integerValue)
switch reasonValue!{
case .PlaybackEnded:
/* The movie ended normally */
print("Playback Ended")
case .PlaybackError:
/* An error happened and the movie ended */
print("Error happened")
case .UserExited:
/* The user exited the player */
print("User exited")
}
print("Finish Reason = \(theReason)")
stopPlayingVideo()
}
}
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
if error == nil {
println("Your altered image has been saved to your photos.")
} else {
println("Save error: \( error?.localizedDescription)")
}
}
func stopPlayingVideo() {
if let player = moviePlayer{
NSNotificationCenter.defaultCenter().removeObserver(self)
player.stop()
player.view.removeFromSuperview()
}
}
func videoThumbnailIsAvailable(notification: NSNotification){
if let _ = moviePlayer{
println("Thumbnail is available")
let thumbnail =
notification.userInfo![MPMoviePlayerThumbnailImageKey] as? UIImage
if let image = thumbnail{
println("Thumbnail image = \(image)")
UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)
}
}
}
func startPlayingVideo(){
let mainBundle = NSBundle.mainBundle()
let url = NSURL(string: "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_5mb.mp4")
if let _ = moviePlayer{
stopPlayingVideo()
}
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer{
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "videoHasFinishedPlaying:",
name: MPMoviePlayerPlaybackDidFinishNotification,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "videoThumbnailIsAvailable:",
name: MPMoviePlayerThumbnailImageRequestDidFinishNotification,
object: nil)
print("Successfully instantiated the movie player")
player.scalingMode = .AspectFit
view.addSubview(player.view)
player.setFullscreen(true, animated: true)
player.play()
let thirdSecondThumbnail = 16.0
player.requestThumbnailImagesAtTimes([thirdSecondThumbnail],
timeOption: .NearestKeyFrame)
} else {
print("Failed to instantiate the movie player")
}
}
override func viewDidLoad() {
super.viewDidLoad()
playButton = UIButton(frame: CGRectMake(0, 0, 77, 77))
playButton.setTitle("Play", forState: UIControlState.Normal)
playButton.titleColorForState(UIControlState.Normal)
playButton.layer.cornerRadius = 7
playButton.backgroundColor = UIColor.redColor()
playButton.center = self.view.center
self.view.addSubview(playButton)
playButton.addTarget(self,
action: "startPlayingVideo",
forControlEvents: .TouchUpInside)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment