【问题标题】:Getting sounds to work right让声音正常工作
【发布时间】:2015-07-21 05:27:29
【问题描述】:

我正在尝试将声音效果添加到我在 Xcode 6 中制作的游戏应用程序中。这是我用来播放声音的代码:

var jumpSoundPlayer:AVAudioPlayer = AVAudioPlayer()
var jumpAudioPath:String = ""
var jumpSoundError: NSError? = nil

func setUpSoundPlayers(){
    jumpAudioPath = NSBundle.mainBundle().pathForResource("Jump", ofType: "mp3")!
    jumpSoundError = nil
    jumpSoundPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: jumpAudioPath), fileTypeHint: "mp3", error: &jumpSoundError)
}

func playJumpSound(volume:Float = 1){
    jumpSoundPlayer.volume = volume
    jumpSoundPlayer.play()
}

我可以播放声音,但似乎在当前播放完成之前它不会播放声音。我怎样才能让它同时播放多个声音并相互重叠?

此外,每次播放声音时,它都会稍微降低帧速率。有办法解决吗?

【问题讨论】:

    标签: swift audio xcode6


    【解决方案1】:

    相信你一定要用

    jumpSoundPlayer.prepareToPlay()
    

    使用前

    jumpSoundPlayer.play()
    

    另外,降低帧速率的唯一解决方案是在使用之前或在游戏开始时设置所有 AVAudioPlayer。

    【讨论】:

      猜你喜欢
      • 2019-03-24
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      • 2022-10-18
      • 2014-09-18
      • 2020-10-21
      相关资源
      最近更新 更多