2016-04-14 110 views
2

我有一个名为'backgroundMusic'的SKSpriteNode的子类,它由SKSpriteNode和AVAudioPlayer文件组成。我的目标是在实例化完成后彻底删除'backgroundMusic'。我试着做:完全删除SKSpriteNode变量

backgroundMusic.removeFromParent() 

但它只能删除SKSpriteNode而不是AVAudioPlayer。

的主要问题是,这个子类的里面,我叫了一堆的功能之外的功能中,当我尝试清空子类:

backgroundMusic = nil 

调用所有功能的过程中仍然是当我重新实例化时会发生并导致问题。我相信会工作的是,如果我完全删除'backgroundMusic',这将停止函数调用过程,然后在需要时重新实例化它,它应该可以正常工作而不会出现问题。我怎样才能做到这一点?

编辑我想:

self.delete(backgroundMusic) 

和坠毁的应用。我应该使用这个吗?如果是这样如何?

回答

0

这是因为您还没有配置Audio Session

用于玩一些代码:从文档

import AVFoundation 

var audioPlayer = AVAudioPlayer() 

func playAudio() { 
    // Set the sound file name & extension 
    let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Flip 02", ofType: "wav")!) 

    // Preperation 
    try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.MixWithOthers) 
    try! AVAudioSession.sharedInstance().setActive(true) 

    // Play the sound 
    do { 
     try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound) 
     audioPlayer.prepareToPlay() 
     audioPlayer.play() 
    } catch { 
     print("there is \(error)") 
    } 
} 

详情:

AVAudioSessionCategoryOptionMixWithOthers

从本次会议音频

混音的音频来自其他活动会话。

仅在会话类别为 AVAudioSessionCategoryPlayAndRecord或AVAudioSessionCategoryPlayback时有效。 (隐如果会话类是AVAudioSessionCategoryAmbient。)

如果您在使用此选项激活您的会话,你的应用程序的音频 不会中断音频从其他应用程序(如音乐应用程序)。如果 未使用此选项(或可隐式混合的类别),则 激活您的会话将会中断其他不可混合的会话。

要停止你可以这样做:

do { 

     try AVAudioSession.sharedInstance().setActive(false) 

    } catch let error as NSError { 

     print(error.localizedDescription) 

    } 
+0

这不是我的问题。我想知道如何在实例化后删除我的变量'backgroundMusic'。音乐很好。 – OriginalAlchemist

+0

你必须提供更多背景音乐代码,你的问题不解释你的音乐会在停止后继续播放。 –