gpt4 book ai didi

ios - 声音在 Sprite Kit 游戏中一直播放

转载 作者:行者123 更新时间:2023-12-03 02:04:47 25 4
gpt4 key购买 nike

我使用此代码在我的 Sprite Kit 游戏中循环播放一首歌曲,但是当我退出应用程序/游戏时,歌曲继续播放,并且我不知道如何在您退出游戏时停止歌曲:

    url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"deep-blue-sea" ofType:@"wav"]];
_sound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
_sound.delegate = self;
_sound.numberOfLoops = -1;
[_sound play];

请问有人可以帮我怎么做吗?

最佳答案

UIApplicationDelegate协议(protocol)有一个名为 applicationWillResignActive: 的方法. The documentation说:

This method is called to let your app know that it is about to move from the active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the app and it begins the transition to the background state. An app in the inactive state continues to run but does not dispatch incoming events to responders.

You should use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. An app in the inactive state should do minimal work while it waits to transition to either the active or background state.


停止音乐的最佳时机! AVAudioPlayer有一个 stop停止播放的方法,就像 play方法开始播放。您在评论中提到您的音频播放器位于 SKScene 中。所以我会在你的场景中创建一个停止音乐的方法:
-(void)stopMusic {
[_player stop];
}
然后调用 AppDelegate中的方法.
-(void)applicationWillResignActive:(UIApplication *)application {
[scene stopMusic]; //scene is your SKScene subclass
}

关于ios - 声音在 Sprite Kit 游戏中一直播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28444224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com