我的 SKAudioNode 有问题。游戏以 Home 键结束。将播放一个音频文件。
当我返回时,音频文件将继续播放。
我想阻止它。
我尝试了很多事情,但没有成功。
self.AudioNodeSet.removeFromParent()
// 不起作用
self.AudioNodeSet.run(SKAction.stop())
// 也不起作用
谁能帮我?
class GameScene: SKScene {
var AudioNodeSet = SKAudioNode()
override func didMove(to view: SKView) {
self.addChild(AudioNodeSet) // Audio Node
AudioNodeSet.name = "AudioNodeSet"
......
func applicationWillResignActive(_ application: UIApplication) {
let imageView = UIImageView(frame: self.window!.bounds)
imageView.tag = 101
imageView.backgroundColor = UIColor.white
//imageView.contentMode = .center
imageView.contentMode = .scaleAspectFill
let pic = UIImage(named: "Start Page.jpg") // Overwrite Return Scene
imageView.image = pic
UIApplication.shared.keyWindow?.subviews.last?.addSubview(imageView)
}
........
override func didMove(to view: SKView) {
scene?.size = CGSize(width: 1920, height: 1080)
scene?.backgroundColor = .black
scene?.scaleMode = .aspectFill
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.willResignActiveNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(appMovedToForeground), name: UIApplication.didBecomeActiveNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(appWillTerminate), name: UIApplication.willTerminateNotification, object: nil)
.........
@objc func appMovedToForeground() {
print("applicationWillResignActive -> appMovedToForeground")
self.AudioNodeSet.run(SKAction.stop()) // not work!
self.AudioNodeSet.removeFromParent() // second try -> not work!
此代码在游戏继续时执行。
然后可以听到音频文件的其余部分。