viktor.radovic
viktor.radovic

Reputation: 518

SKAction play sound not from begining

I want to play sound not from the beginning of the mp3 file, but from 14s after beginning. I have the following code for sound playing, but i was not successful in finding the solution:

if let textAudio = textAudio {
        let wait = SKAction.wait(forDuration: 0.1)
        let play = SKAction.play()
        if view.scene! == scene02 {
            // play 14s after begining
        }
        textAudio.run(SKAction.sequence([wait, play]))
    }

Upvotes: 2

Views: 63

Answers (1)

Alessandro Ornano
Alessandro Ornano

Reputation: 35372

You can use AVAudioPlayer and the currentTime method as explained here:

import AVFoundation
var textAudio: AVAudioPlayer?
let path = Bundle.main.path(forResource: "example.mp3", ofType:nil)!
let url = URL(fileURLWithPath: path)

do {
    textAudio = try AVAudioPlayer(contentsOf: url)
    textAudio?.currentTime = TimeInterval(14.0)
    textAudio?.play()
} catch {
    // couldn't load file :(
}

Upvotes: 3

Related Questions