声音在 Sprite Kit 游戏中持续播放
Sound keeps playing on Sprite Kit game
我使用此代码在我的 Sprite Kit 游戏中循环播放一首歌曲,但是当我离开 app/game 时,歌曲一直在播放,我不知道如何在你离开时停止这首歌游戏:
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
协议有一个名为 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
}
我使用此代码在我的 Sprite Kit 游戏中循环播放一首歌曲,但是当我离开 app/game 时,歌曲一直在播放,我不知道如何在你离开时停止这首歌游戏:
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
协议有一个名为 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
}