【问题标题】:Playing Two Sounds in iOS Using Swift 2.1使用 Swift 2.1 在 iOS 中播放两种声音
【发布时间】:2016-02-09 15:42:24
【问题描述】:

我有一个包含选择轮和播放按钮的应用。当用户选择想要的声音并点击播放按钮时,声音就会播放。除此之外,我还有一个“随机”按钮,它将生成一个随机数,将选择轮移动到适当的数组索引,然后播放声音。所有这些工作正常。

问题是我正在尝试添加一个“组合”按钮,该按钮基本上与“随机”按钮的作用相同,但我希望它播放 2 或 3 个声音,而不是只播放 1 个声音。

这是我现有的代码(playComboSound 函数的问题):

// Play Audio
var audioPlayer = AVAudioPlayer()

func playAudio() {
    do {
        if let bundle = NSBundle.mainBundle().pathForResource(Sounds[selection], ofType: "mp3") {
            let alertSound = NSURL(fileURLWithPath: bundle)
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)
            try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound)
            audioPlayer.prepareToPlay()
            audioPlayer.play()
        }
    } catch {
        print(error)
    }
}

@IBAction func playRandomSound(sender: AnyObject) {

    // Generate Random Number Based on SoundNames Array Count and assign to Selection
    let randomNumber = Int(arc4random_uniform(UInt32(SoundNames.count)))
    selection = randomNumber

    // Move Picker Wheel to Match Random Number and Play Sound
    self.picker.selectRow(selection, inComponent: 0, animated: true)
    playAudio()
}

@IBAction func playComboSound(sender: AnyObject) {
    // Generate Random Number Based on SoundNames Array Count and assign to Selection
    let randomNumber1 = Int(arc4random_uniform(UInt32(SoundNames.count)))
    selection = randomNumber1

    // Move Picker Wheel to Match Random Number and Play Sound
    self.picker.selectRow(selection, inComponent: 0, animated: true)
    playAudio()

    // Generate Random Number Based on SoundNames Array Count and assign to Selection
    let randomNumber2 = Int(arc4random_uniform(UInt32(SoundNames.count)))
    selection = randomNumber2

    // Move Picker Wheel to Match Random Number and Play Sound
    self.picker.selectRow(selection, inComponent: 0, animated: true)
    playAudio()
}

当我单击 playComboSound 按钮时,它的作用基本上与 playRandomSound 按钮相同。移动选择轮并播放声音,但它只执行一次而不是两次。如果我在两者之间进行睡眠,我会在播放两个声音方面取得一定程度的成功,但它似乎只移动了一次选择轮(对于第二个声音)。任何帮助将不胜感激!谢谢。

【问题讨论】:

  • @TamasZahola 很好的答案!基本上你需要创建第二个音频播放器...audioPlayer2 这样你就可以一次播放两个声音。否则,当你去播放第二个声音时,第一个会停止并被第二个替换。

标签: ios iphone xcode avaudioplayer


【解决方案1】:

移动选择轮并播放声音,但它只执行一次而不是两次。

它会播放一次声音,因为第二次调用 playAudio 会使用新的 AVAudioPlayer 实例覆盖共享的 audioPlayer 变量,该实例会释放第一个实例。您应该保留对所有活动音频播放器的引用数组/集合,以防止它们被释放,并且只有在它们完成后才将它们从该集合中删除(使用AVAudioPlayerDelegate 回调来检测播放器何时完成)。

如果我在两者之间进行睡眠,我会在播放两个声音方面取得一定程度的成功,但它似乎只移动了一次选择轮(对于第二个声音)。

睡眠有帮助,因为当主线程处于睡眠状态时,AVFoundation 的线程有机会运行,它们可以开始播放第一个 AVAudioPlayer,然后第二次调用 playAudio 覆盖它并释放它。不过,您在这里依赖于未定义的行为。

它似乎只移动一次选择轮(第二个声音)

这是预期的行为。在选择轮上只能选择一个项目。对selectRow 的第二次调用会覆盖第一次调用。

【讨论】:

  • 感谢 Tamas 的快速回复。我不确定你的意思是什么:“你应该保留一个数组/一组对所有活动音频播放器的引用,以防止它们被释放,并且只有在它们完成后才从这个集合中删除它们(使用 AVAudioPlayerDelegate 回调检测玩家何时完成)。”我应该创建两个单独的音频播放器吗? (audioPlayer1, audioPlayer2)?
  • 您应该有一组或数组 AVAudioPlayers 而不是单个字段:var audioPlayers:[AVAudioPlayer] = []。然后在playAudio() 中创建一个AVAudioPlayer 并将其添加到audioPlayers:self.audioPlayers.append(audioPlayer)。播放完成后,您应将它们从audioPlayers 中删除,以免内存泄漏。这可以通过AVAudioPlayerDelegate完成/错误回调来实现:developer.apple.com/library/ios/documentation/AVFoundation/…
猜你喜欢
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 2014-10-20
  • 2015-12-25
  • 2014-12-26
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
相关资源
最近更新 更多