如何播放/停止背景音乐和声音效果?

时间:2016-02-23 20:40:29

标签: ios swift sprite-kit avaudioplayer

我有2种音效,我无法阻止它们,但我的背景音乐效果很好。

我的声音效果属于不同的类 - 他们在GameScene我从那里运行它们。

我如何才能正确停止声音效果?

例如:我有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")
            }
        }

1 个答案:

答案 0 :(得分:0)

所以创建一个声音播放单身人士。给它开始和停止2种声音效果的方法。由于它是一个单身人士,你可以在你的程序中的任何地方调用它。