【问题标题】:SKAction play sound not from beginingSKAction 播放声音不是从头开始的
【发布时间】:2017-12-06 03:49:55
【问题描述】:

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

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

【问题讨论】:

  • 据我所知,SKAction 无法做到这一点。
  • 如果你想坚持使用SpriteKit 框架,你需要使用SKAudioNodeAVAudioPlayerNode。为这个写一个答案需要太长时间,而且我不知道有任何解释这个机制的教程,也许其他人可以添加到这个评论中。

标签: ios iphone swift audio sprite-kit


【解决方案1】:

您可以按照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 :(
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-20
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多