菜鸟教程小白 发表于 2022-12-12 15:11:27

ios - AVFoundation 视频播放问题


                                            <p><p>我跟着 <a href="https://www.youtube.com/watch?v=3FKzSYAGfJ0" rel="noreferrer noopener nofollow">this excellent tutorial</a>来自 Yuma 并且有一些工作。下面是我在 ViewController 中使用的代码</p>

<pre><code>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&#39;s delegate
      sceneView.delegate = self

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

      // Create a new scene
      let scene = SCNScene(named: &#34;art.scnassets/notebook.scn&#34;)!

      // 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: &#34;AR Resources&#34;, bundle: nil) else { return }
      configuration.trackingImages = arImages
      // Run the view&#39;s session
      sceneView.session.run(configuration)
    }

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

      // Pause the view&#39;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: &#34;amy&#34;, recursively: false) else { return }
      amyContainer.removeFromParentNode()
      node.addChildNode(amyContainer)
      amyContainer.isHidden = false

      // Video
      let videoURL = Bundle.main.url(forResource: &#34;video&#34;, withExtension: &#34;mp4&#34;)!

      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: &#34;video&#34;, recursively: true)
      else { return }
    video.geometry?.firstMaterial?.diffuse.contents = videoScene

}
}
</code></pre>

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

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

<p>对于这种方法,mp4 是否需要特定的规则/设置,我可以使用 Premiere Pro 或 Handbrake 中的设置吗?或者如何调试通过 Xcode 本身发生的任何内部错误?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>可能是因为您的视频很大。在尝试播放视频之前,您需要先等待 <strong>AVPlayer</strong> 准备就绪</p>

<pre><code>class ViewController: UIViewController, ARSCNViewDelegate {

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

    override func viewDidLoad() {
      super.viewDidLoad()

      // Set the view&#39;s delegate
      sceneView.delegate = self

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

      // Create a new scene
      let scene = SCNScene(named: &#34;art.scnassets/notebook.scn&#34;)!

      // 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: &#34;AR Resources&#34;, bundle: nil) else { return }
      configuration.trackingImages = arImages
      // Run the view&#39;s session
      sceneView.session.run(configuration)
    }

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

      // Pause the view&#39;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: &#34;amy&#34;, 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: &#34;video&#34;, recursively: true)
            else { return }
      video.geometry?.firstMaterial?.diffuse.contents = videoScene

    }

    func createAVPlayer() -&gt; AVPlayer {
      let videoURL = Bundle.main.url(forResource: &#34;video&#34;, withExtension: &#34;mp4&#34;)!
      let asset = AVAsset(url: videoURL)

      let assetKeys = [
            &#34;playable&#34;,
            &#34;hasProtectedContent&#34;
      ]
      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)
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - AVFoundation 视频播放问题,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/55301481/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/55301481/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - AVFoundation 视频播放问题