在Swift Playground中使用AVPlayer播放video?
我在使用AVPlayer在快速游乐场内播放video时遇到问题。
这是我的代码。
import UIKit import AVFoundation var f=CGRectMake(0, 0, 500, 500) var url=NSURL(string: "http://s3.amazonaws.com/vids4project/sample.mp4") var playerItem = AVPlayerItem(URL: url) var v = UIView(frame:f) var player=AVPlayer(playerItem: playerItem) var playerLayer=AVPlayerLayer(player: player) playerLayer.frame=f v.layer.addSublayer(playerLayer) player.play()
有什么建议么? 代码一点都不做。 我的期望是’v’变量应该显示video。 当我将avplayerlayer连接到故事板的视图时,它似乎在操场外工作。
如果将它放入操场,以下情况应该有效。 确保换出路径:(Xcode7 GM)
import UIKit import AVFoundation import XCPlayground var f = CGRectMake(0, 0, 500, 500) let path = NSBundle.mainBundle().pathForResource("movie", ofType: "m4v")! let url = NSURL(fileURLWithPath: path) var playerItem = AVPlayerItem(URL: url) var v = UIView(frame: f) v.backgroundColor = UIColor.blackColor() var player = AVPlayer(playerItem: playerItem) var playerLayer = AVPlayerLayer(player: player) playerLayer.frame = f v.layer.addSublayer(playerLayer) player.play() XCPShowView("View", view: v) XCPSetExecutionShouldContinueIndefinitely(true)
尝试在最后添加此项以保持游乐场的运行
import XCPlayground XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)
感谢@Justin Levi Winter的回答,我更新了Swift3的代码,使用Xcode 8测试(此video在时间轴中播放,但不在快速查看中):
import AVFoundation import PlaygroundSupport URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil) let width = 568 let height = 320 let container = UIView(frame: CGRect(x: 0, y: 0, width: width, height: height)) PlaygroundPage.current.liveView = container PlaygroundPage.current.needsIndefiniteExecution = true func playVideo(_ url: URL){ let f=CGRect(x: 0, y: 0, width: width, height: height) let playerItem = AVPlayerItem(url: url) let player=AVPlayer(playerItem: playerItem) let playerLayer=AVPlayerLayer(player: player) playerLayer.frame=f container.layer.addSublayer(playerLayer) PlaygroundPage.current.liveView = container player.play() } playVideo(URL(string:"http://s3.amazonaws.com/vids4project/sample.mp4")!)
- Swift:影响button点击的UITextfield? 由于animation无法input文字?
- 为什么在创buildUIWebView时,清除NSUserDefaults会导致EXC_CRASH?
- 找不到“RevMobAds / RevMobAds.h”文件
- 随机数列表 – arc4random
- Objective C可以在运行时(reflection)确定调用者对象引用吗?
- 大量内存消耗:ImageIO_jpeg_Data
- UICollectionView插入上面维护位置的单元格(如Messages.app)
- Realm没有用空格查询string字段
- :发送到实例0x6d21350的无法识别的select器