簡體   English   中英

我可以在停止后使用.playAudio() 恢復播放嗎?

[英]Can I use .playAudio() to resume playback after stopping?

從這個問題如果我在 Reality Composer 中分配聲音,我可以在 RealityKit 中以編程方式停止它嗎? ,我想在Play Music后使用方法恢復播放。

我可以這樣做嗎?

現在,我在 stopAudio function 中使用這個命令來停止音樂。

func stopAudio() {
    if arView.scene.anchors.count > 0 {
        if arView.scene.anchors[0].isAnchored {
            arView.scene.anchors[0].children[0].stopAllAudio()
        }
    }
}

如果我想讓arView.scene.anchors[0]重新播放音樂,我應該使用哪個命令?

音頻播放 Controller

由於 RealityKit 2.0 無法控制 Reality Composer 行為的參數,因此控制音頻的最佳策略是創建一個程序化的AudioPlaybackController 要將音頻文件提供給 controller, .rcproject場景導出為.usdz格式並使用解壓縮技巧提取.caf聲音文件。 您可以選擇spatialnon-spatial音頻體驗。

UIKit版本

import UIKit
import RealityKit

extension ViewController {

    private func loadAudio() {
        do {
            let resource = try AudioFileResource.load(
                                                named: "planetarium07.caf",
                                                   in: nil,
                                            inputMode: .spatial,
                                      loadingStrategy: .preload,
                                           shouldLoop: true)

            self.controller = entity.prepareAudio(resource)
            self.controller?.speed = 0.9
            self.controller?.fade(to: .infinity, duration: 2)

        } catch {
            print(error.localizedDescription)
        }
    }
}

視圖控制器。

class ViewController : UIViewController {
    
    @IBOutlet var uiView: UIView!         // when using @IBAction buttons
    @IBOutlet var arView: ARView!
    private var entity = Entity()
    private var controller: AudioPlaybackController? = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
        uiView.backgroundColor = .systemCyan
        
        let boxScene = try! Experience.loadBox()
        arView.scene.anchors.append(boxScene)
        
        let anchor = boxScene.anchor
        anchor?.addChild(entity)
        
        self.loadAudio()
    }
    @IBAction func playMusic(_ sender: UIButton) {
        self.controller?.play()
    }
    @IBAction func stopMusic(_ sender: UIButton) {
        self.controller?.pause()
        // self.controller?.stop()
    }
}

SwiftUI版本

import SwiftUI
import RealityKit

struct ContentView : View {       
    @State var arView = ARView(frame: .zero)
    @State var controller: AudioPlaybackController? = nil
    @State var entity = Entity()
    
    var body: some View {
        ZStack {
            ARViewContainer(arView: $arView, 
                            entity: $entity).ignoresSafeArea()
            VStack {
                Spacer()
                Button("Play") { loadSound(); controller?.play() }
                Button("Stop") { controller?.stop() }
            }
        }
    }
    func loadSound() {
        do {
            let resource = try AudioFileResource.load(
                                                named: "planetarium07.caf",
                                                   in: nil,
                                            inputMode: .spatial,
                                      loadingStrategy: .preload,
                                           shouldLoop: true)    
            self.controller = entity.prepareAudio(resource)
        } catch {
            print(error.localizedDescription)
        }
    }
}

ARView容器。

struct ARViewContainer: UIViewRepresentable {
    
    @Binding var arView: ARView
    @Binding var entity: Entity
    
    func makeUIView(context: Context) -> ARView {

        let boxScene = try! Experience.loadBox()
        arView.scene.anchors.append(boxScene)
        
        let anchor = boxScene.anchor
        anchor?.addChild(entity)
        return arView
    }
    func updateUIView(_ view: ARView, context: Context) { }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM