我有两个声音效果,我不能阻止他们,但我的背景音乐作品完美。
我的音效是在不同的类-他们在游戏场景我运行他们从那里。
如何才能正确地停止音效?
例如:我有CatchObjectSound,我创建了所需的函数。但当我点击按钮时,我仍然能听到声音效果。
我想做什么?
我想停止我的音效。
SKTAudio等级:

import UIKit
import AVFoundation

public class Singleton {


    // Initialize
     public var backgroundMusicPlayer: AVAudioPlayer?
     public var soundEffectCatchObject: AVAudioPlayer?
     //public var soundEffectGameOver: AVAudioPlayer?
     //public var soundEffectClickedButton: AVAudioPlayer?



    public class func sharedInstance() -> Singleton {
        return SingletonInstance
    }


    // ---------------------------------------------
    // Play Background Music
    // ---------------------------------------------

    func playBackgroundMusic() {
        let url = NSBundle.mainBundle().URLForResource("Unity.mp3", withExtension: nil)
        guard let newURL = url else {
            print("Could not find file: \("Unity.mp3")")
            return
        }
        do {
            backgroundMusicPlayer = try AVAudioPlayer(contentsOfURL: newURL)
            backgroundMusicPlayer!.numberOfLoops = -1
            backgroundMusicPlayer!.prepareToPlay()
            backgroundMusicPlayer!.play()
        } catch let error as NSError {
            print(error.description)
        }
    }

    func pauseBackgroundMusic() {
        backgroundMusicPlayer!.pause()
    }

    func resumeBackgroundMusic() {
        backgroundMusicPlayer!.play()
    }


    // ---------------------------------------------
    // Play Sound Effect Catch Object
    // ---------------------------------------------

    func playSoundEffectCatchObject() {
        let url = NSBundle.mainBundle().URLForResource("Magical Echo Spell 03.wav", withExtension: nil)
        guard let newURL = url else {
            print("Could not find file: \("Magical Echo Spell 03.wav")")
            return
        }
        do {
            let error: NSError? = nil
            soundEffectCatchObject = try! AVAudioPlayer(contentsOfURL: newURL)
            if let player = soundEffectCatchObject {
            player.numberOfLoops = 0
            player.prepareToPlay()
            player.play()
            } else {
            print("Could not create audio player: \(error!)")
        }
    }

    func pauseSoundEffectCatchObject() {
        if let player = soundEffectCatchObject {
        if player.playing {
           player.pause()
        }
    }
}

    func resumeSoundEffectCatchObject() {
        if let player = soundEffectCatchObject {
            if player.playing {
               player.play()
    }
            }
        }
    }
}

let SingletonInstance = Singleton()

我有主场景中的暂停/恢复按钮。
这个按钮工作得很好,但我无法停止我的声音效果,我试过了。
主要场景
if(SoundOnOff.containsPoint(location)) {

            if  (Singleton.sharedInstance().backgroundMusicPlayer?.playing == true) {
                Singleton.sharedInstance().pauseBackgroundMusic()
               Singleton.sharedInstance().pauseSoundEffectCatchObject()
                self.SoundOnOff.texture = SKTexture(imageNamed:"MusicOff.png")

            } else {
               Singleton.sharedInstance().resumeBackgroundMusic()
               Singleton.sharedInstance().resumeSoundEffectCatchObject()
                self.SoundOnOff.texture = SKTexture(imageNamed:"MusicOn.png")
            }
        }

最佳答案

所以创造一个单曲播放的声音。给它方法来启动和停止2个声音效果。因为它是一个单变量,所以你可以在程序中的任何地方调用它。

10-08 07:22