Last active
January 5, 2016 20:52
-
-
Save norio-nomura/68c0b3dd2b2939fb7eb8 to your computer and use it in GitHub Desktop.
PlaygroundでAVPlayerLayer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Require "Run in Full Simulator" in Playground Settings. | |
// On first execution, iOS Simulator will show request authorization for Photo Library. | |
// Only a video without audio track can be played. | |
import UIKit | |
import AVFoundation | |
import Photos | |
import XCPlayground | |
class PLayerView: UIView { | |
override class func layerClass() -> AnyClass { | |
return AVPlayerLayer.self | |
} | |
var player: AVPlayer { | |
let playerLayer = layer as AVPlayerLayer | |
if playerLayer.player == nil { | |
playerLayer.player = AVPlayer() | |
} | |
return playerLayer.player | |
} | |
} | |
let frame = CGRect(x: 0, y: 0, width: 480, height: 320) | |
let view = PLayerView(frame: frame) | |
XCPShowView("view", view) | |
PHPhotoLibrary.requestAuthorization { | |
(status) in | |
let videos = PHAsset.fetchAssetsWithMediaType(.Video, options: nil) | |
if let asset = videos.firstObject as? PHAsset { | |
let manager = PHImageManager.defaultManager() | |
manager.requestPlayerItemForVideo(asset, options: nil) { | |
(item, info) in | |
view.player.replaceCurrentItemWithPlayerItem(item) | |
view.player.play() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment