在斯威夫特游乐场和AVPlayer玩视频?

时间:2023-01-23 23:20:34

I am having trouble playing a video within a swift playground using AVPlayer.

我在使用AVPlayer快速播放视频时遇到了麻烦。

Here's my code.

这是我的代码。

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()

Any suggestions? The code as does nothing at all. My expectation is the 'v' variable should be showing the video. It seems to work outside the playground when I am connecting the avplayerlayer to a view from a storyboard.

有什么建议吗?代码什么都不做。我的期望是v变量应该显示视频。当我将avplayerlayer连接到故事板上的视图时,它似乎在操场外工作。

3 个解决方案

#1


7  

The following should work if you drop it into a playground. Make sure to swap out the path: (Xcode7 GM)

如果你把它扔到操场上,下面的方法应该是有效的。确保切换路径:(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)

#2


6  

Try adding this at the end to keep the playground running

试着在最后添加这个来保持操场的运转

import XCPlayground
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)

#3


0  

Thanks of @Justin Levi Winter's answer and I had updated the code for Swift3, tested with Xcode 8 (This video plays in timeline, but not on the Quick Look):

感谢@Justin Levi Winter的回答,我已经更新了Swift3的代码,用Xcode 8进行了测试(这个视频在时间轴上播放,但不是快速浏览):

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")!)

#1


7  

The following should work if you drop it into a playground. Make sure to swap out the path: (Xcode7 GM)

如果你把它扔到操场上,下面的方法应该是有效的。确保切换路径:(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)

#2


6  

Try adding this at the end to keep the playground running

试着在最后添加这个来保持操场的运转

import XCPlayground
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)

#3


0  

Thanks of @Justin Levi Winter's answer and I had updated the code for Swift3, tested with Xcode 8 (This video plays in timeline, but not on the Quick Look):

感谢@Justin Levi Winter的回答,我已经更新了Swift3的代码,用Xcode 8进行了测试(这个视频在时间轴上播放,但不是快速浏览):

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")!)