我在使用AVPlayer在快速游乐场内播放视频时遇到问题.
这是我的代码.
import UIKit import AVFoundation var f=CGRectMake(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’变量应该显示视频.当我将avplayerlayer连接到故事板的视图时,它似乎在操场外工作.
解决方法
如果将它放入操场,以下情况应该有效.确保换出路径:(Xcode7 GM)
import UIKit import AVFoundation import XCPlayground var f = CGRectMake(0,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)