Home > Enterprise >  Can I use .playAudio() to resume playback after stopping?
Can I use .playAudio() to resume playback after stopping?

Time:01-07

From the question in this If I assign a sound in Reality Composer, can I stop it programmatically in RealityKit?, I would like to use method to resume playback after Play Music.

Can I do that?

Now, I use this command in stopAudio function to stop the music.

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

If I want arView.scene.anchors[0] to replay the music again, which command should I use?

CodePudding user response:

Audio Playback Controller

Since RealityKit 2.0 isn't able to control parameters of Reality Composer's behaviors, the best strategy for controlling audio is to create a programmatic AudioPlaybackController. To feed your audio file to the controller, export .rcproject scene to .usdz format and use unzipping trick to extract the .caf sound file.

Here's the code:

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

ViewController.

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

CodePudding user response:

To resume playback of a sound in Reality Composer after pausing it, you can use the resume() method of the AudioPlayer class. Here's an example of how you might use this method in RealityKit:

import RealityKit

// Create an audio player and load a sound file
let audioPlayer = AudioPlayer()
let audioFile = try! AudioFile.load(named: "sound.mp3")

// Assign the audio file to the player
audioPlayer.assign(audioFile)

// Start playing the sound
audioPlayer.play()

// ...

// To pause the sound, call the pause() method
audioPlayer.pause()

// ...

// To resume playback, call the resume() method
audioPlayer.resume()
  • Related