SKAction播放的声音不是从开始播放的

时间:2017-12-06 03:49:55

标签: ios iphone swift audio sprite-kit

我想播放的声音不是从mp3文件的开头,而是从开始后的14s播放。我有以下声音播放代码,但我没有成功找到解决方案:

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

1 个答案:

答案 0 :(得分:3)

您可以按照here的说明使用AVAudioPlayercurrentTime方法:

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