Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PlaygroundでAVPlayerLayer
// 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
You can’t perform that action at this time.