简体   繁体   English

如何停止使用SpriteKit和Swift播放声音效果?

[英]How do I stop playing a sound effect with SpriteKit and Swift?

I'm writing a storybook app for my niece and I have a question about SpriteKit. 我正在为侄女写一个故事书应用,但我对SpriteKit有疑问。 I'm trying to set it so that there are different types of audio that play. 我正在尝试将其设置为播放不同类型的音频。

  1. Background music that loops (AVFoundation) 循环播放的背景音乐(AVFoundation)
  2. Narration that plays when on a new page, or when you press the narrate button to replay the narration (SKAction) 在新页面上或在您按下旁白按钮以重新播放旁白时播放的旁白(SKAction)

My problem is that the narration will play on top of each other if the user changes the page, or if the user plays the replay narration button. 我的问题是,如果用户更改页面或用户播放重播旁白按钮,则旁白将在彼此之上播放。 So it ends up sounding like two people talking over each other. 因此,听起来好像两个人互相交谈。

How can I stop all narrations that are playing when a new narration is triggered? 触发新的旁白时,如何停止正在播放的所有旁白?

I can't find any relevant help on the internet. 我在互联网上找不到任何相关帮助。 I've seen some posts saying us AVFoundation, but from my understanding (albeit limited) that seems more for the background music and can only have one track playing. 我看过一些帖子说我们是AVFoundation,但据我了解(尽管有限),对于背景音乐来说似乎更多,只能播放一个曲目。

Am I misinterpreting the documentation? 我会误解文档吗? Can someone help me answer this problem? 有人可以帮我回答这个问题吗?

import SpriteKit

import AVFoundation 导入AVFoundation

class Page1: SKScene { 类别Page1:SKScene {

// MARK: Touch handling

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        println("\(location)")

        //checks if someone touched the menu button
        if menuButton.containsPoint(location) {
            println("Play story with sound!")
            settings.setInProgress()
            goToPauseMenu()
        }

        if soundButton.containsPoint(location) {
            println("Play story with sound!")
            settings.setInProgress()
            runAction(playNar)
        }

        //checks if someone touched the forward button
        if pageForward.containsPoint(location) {
            println("Next Page!")
            settings.setInProgress()
            nextPage()
        }

    }
}

Your readings suggesting use of AVFoundation are good advice. 您的阅读建议使用AVFoundation是很好的建议。 I wouldn't characterize AVFoundation as just for background music though. 我不会将AVFoundation的特征仅仅描述为背景音乐。

Yes, you can only play one stream at a time with an AVAudioPlayer instance, but you can have multiple instances of AVAudioPlayers playing different (or the same) streams all overlapping if your needs require it. 是的,您只能一次使用AVAudioPlayer实例播放一个流,但是如果需要,您可以让多个AVAudioPlayers实例播放不同(或相同)的流,这些流全部重叠。 SpritKit's audio actions, relative to AVFoundation's controls, are so extremely limited that they're almost useless for all but the most basic requirements. 相对于AVFoundation的控件,SpritKit的音频操作非常有限,以至于除了最基本的要求外,它们几乎没有用。

I have found myself, early in a project utilizing SKActions for sounds, but inevitably I need finer grained control of my audio (ie volume control, stopping it mid playback, etc..) and end up utilizing AVAudioPlayer. 我早在一个使用SKActions进行声音的项目中就发现自己,但是不可避免地我需要对音频进行更细粒度的控制(例如,音量控制,在播放中停止播放等),最后使用AVAudioPlayer。

如果要使用Sprite Kit动作停止声音效果,则需要做的是在主节点上添加一个子节点,让该子节点播放音频,然后暂停该子节点本身

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM