在 Swift Playground 中使用 AVPlayer 播放视频?

Posted

技术标签:

【中文标题】在 Swift Playground 中使用 AVPlayer 播放视频?【英文标题】:Play a video with AVPlayer in Swift Playground? 【发布时间】:2014-09-20 05:46:30 【问题描述】:

我在使用 AVPlayer 的 Swift Playground 中播放视频时遇到问题。

这是我的代码。

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

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

【问题讨论】:

【参考方案1】:

如果你把它放到操场上,以下应该可以工作。确保换出路径:(Xcode 11 & Swift Playgrounds Mac & ios)

import UIKit
import AVFoundation
import PlaygroundSupport

var playerFrame = CGRect(x: 0, y: 0, width: 500, height: 500)

guard let path = Bundle.main.path(forResource: "movie", ofType: "m4v") else 
    fatalError("no movie found")


let url = URL(fileURLWithPath: path)

var playerItem = AVPlayerItem(url: url)

var playerView = UIView(frame: playerFrame)
playerView.backgroundColor = .black
var player = AVPlayer(playerItem: playerItem)
var playerLayer = AVPlayerLayer(player: player)

playerLayer.frame = playerFrame
playerView.layer.addSublayer(playerLayer)
player.play()

PlaygroundPage.current.liveView = playerView

PlaygroundPage.current.needsIndefiniteExecution = true

【讨论】:

谢谢!如何让它继续播放?【参考方案2】:

尝试在最后添加这个以保持操场运行

import XCPlayground
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)

【讨论】:

【参考方案3】:

感谢@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")!)

【讨论】:

【参考方案4】:

对于 Swift 5.*

在阅读了包括上述答案在内的几个答案后,我编写了以下代码。 它可以工作,但问题播放器视图位置有点偏离。我的意思是当它的框架的 x 和 y 值应该在左上角但它不坐在那里。顺便说一句,主要问题是如何编写url。

import UIKit
import PlaygroundSupport
import AVFoundation
import CoreMedia


class MyViewController : UIViewController 

    var mPlayer : AVPlayer?
    var mItem   : AVPlayerItem?
    var playerLayer        : AVPlayerLayer?
    var playerView = UIView()  
  


    override func loadView() 
    
        let view = UIView()
        self.view = view
        view.frame = CGRect(x: 0, y: 0, width: 1000, height: 1000)
        view.backgroundColor = .darkGray
    
        let playerFrame = CGRect(x: 1, y: 10, width: 500, height: 500)
        playerView.frame =  playerFrame
        view.addSubview(playerView)
        playVideo()
   
    

   func playVideo()
        let url0 =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("IMG_0626.MOV")
    
    
        mItem                       = AVPlayerItem(url: url0)
        mPlayer                     = AVPlayer(playerItem: mItem!)
        playerLayer                 = AVPlayerLayer(player: mPlayer) 
    
        playerView.layer.addSublayer(playerLayer!)
        playerLayer?.frame          = playerView.bounds
        playerLayer?.videoGravity   = AVLayerVideoGravity.resizeAspect
        mPlayer?.play()
    




// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
PlaygroundPage.current.needsIndefiniteExecution = true

【讨论】:

以上是关于在 Swift Playground 中使用 AVPlayer 播放视频?的主要内容,如果未能解决你的问题,请参考以下文章

在 Swift/Playground 中使用 .append 选项到数组结束时出现错误

如何在 Playground 中导入 Swift 包?

如何在 Swift Playground 中使用更新功能

在 Swift 4 Playground 中使用外部库

如何使用 Swift 3 Playground 读取 Resources 目录中的文本文件?

可以在 Swift Playground 中使用 Core Data 吗?