• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - AVFoundation 视频播放问题

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:11:27 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我跟着 this excellent tutorial来自 Yuma 并且有一些工作。下面是我在 View Controller 中使用的代码

import UIKit
import SceneKit
import ARKit
import AVFoundation
import SpriteKit

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet var sceneView: ARSCNView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set the view's delegate
        sceneView.delegate = self

        // Show statistics such as fps and timing information
        sceneView.showsStatistics = true

        // Create a new scene
        let scene = SCNScene(named: "art.scnassets/notebook.scn")!

        // Set the scene to the view
        sceneView.scene = scene
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Create a session configuration
        let configuration = ARImageTrackingConfiguration()

        guard let arImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else { return }
        configuration.trackingImages = arImages
        // Run the view's session
        sceneView.session.run(configuration)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        // Pause the view's session
        sceneView.session.pause()
    }
    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        guard anchor is ARImageAnchor else { return }

        // Amy Image recognition
        guard let amyContainer = sceneView.scene.rootNode.childNode(withName: "amy", recursively: false) else { return }
        amyContainer.removeFromParentNode()
        node.addChildNode(amyContainer)
        amyContainer.isHidden = false

        // Video
        let videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4")!

        let videoPlayer = AVPlayer(url: videoURL)

        let videoScene = SKScene(size: CGSize(width: 900.0, height: 1400.0))

        let videoNode = SKVideoNode(avPlayer: videoPlayer)

        videoNode.position = CGPoint(x: videoScene.size.width / 2, y: videoScene.size.height / 2)

        videoNode.size = videoScene.size

        videoNode.yScale = -1

        videoNode.play()

        videoScene.addChild(videoNode)

        guard let video = amyContainer.childNode(withName: "video", recursively: true)
        else { return }
    video.geometry?.firstMaterial?.diffuse.contents = videoScene

 }
 }

当我使用作为教程一部分提供的视频时,一切正常,但当我尝试包含我创建的视频时,我只会在目标图像上看到一个白框,但没有视频。

我已经检查了三倍的名称,我尝试导出具有完全相同尺寸的视频,但都没有成功。我已经测试过上传一个新版本的视频,它确实可以作为不同的文件名工作,并且更改了名称并且它可以工作,所以我必须假设这是我正在制作的 .mp4 的问题。

对于这种方法,mp4 是否需要特定的规则/设置,我可以使用 Premiere Pro 或 Handbrake 中的设置吗?或者如何调试通过 Xcode 本身发生的任何内部错误?



Best Answer-推荐答案


可能是因为您的视频很大。在尝试播放视频之前,您需要先等待 AVPlayer 准备就绪

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet var sceneView: ARSCNView!
    var observer: NSKeyValueObservation?
    var videoNode: SKVideoNode?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set the view's delegate
        sceneView.delegate = self

        // Show statistics such as fps and timing information
        sceneView.showsStatistics = true

        // Create a new scene
        let scene = SCNScene(named: "art.scnassets/notebook.scn")!

        // Set the scene to the view
        sceneView.scene = scene
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Create a session configuration
        let configuration = ARImageTrackingConfiguration()

        guard let arImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else { return }
        configuration.trackingImages = arImages
        // Run the view's session
        sceneView.session.run(configuration)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        // Pause the view's session
        sceneView.session.pause()
    }
    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        guard anchor is ARImageAnchor else { return }

        // Amy Image recognition
        guard let amyContainer = sceneView.scene.rootNode.childNode(withName: "amy", recursively: false) else { return }
        amyContainer.removeFromParentNode()
        node.addChildNode(amyContainer)
        amyContainer.isHidden = false

        let videoPlayer = self.createAVPlayer()

        let videoScene = SKScene(size: CGSize(width: 900.0, height: 1400.0))

        self.videoNode = SKVideoNode(avPlayer: videoPlayer)

        self.videoNode?.position = CGPoint(x: videoScene.size.width / 2, y: videoScene.size.height / 2)

        self.videoNode?.size = videoScene.size

        self.videoNode?.yScale = -1

        videoScene.addChild(self.videoNode!)

        guard let video = amyContainer.childNode(withName: "video", recursively: true)
            else { return }
        video.geometry?.firstMaterial?.diffuse.contents = videoScene

    }

    func createAVPlayer() -> AVPlayer {
        let videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4")!
        let asset = AVAsset(url: videoURL)

        let assetKeys = [
            "playable",
            "hasProtectedContent"
        ]
        let playerItem = AVPlayerItem(asset: asset,
                                      automaticallyLoadedAssetKeys: assetKeys)

        self.observer = playerItem.observe(\.status, options:  [.new, .old], changeHandler: { (playerItem, change) in
            if playerItem.status == .readyToPlay {

                self.videoNode?.play() // Only play when Its ready
            }
        })

        return AVPlayer(playerItem: playerItem)
    }
}

关于ios - AVFoundation 视频播放问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55301481/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap