您可以使用NSUserDefaults的保存音乐播放状态,这样的观点在加载时,音乐将只如果有尚未发挥。
if ([[NSUserDefaults standardUserDefaults] stringForKey:@"music"] == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
theAudio.delegate = self;
theAudio.numberOfLoops = -1;
[theAudio play];
[[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; }
当退出应用程序时,您需要将此键重置为零。否则,如果玩家退出应用程序,并且下次重新启动应用程序时,@“music”字符串不会为零。
转到projectAppDelegate.m文件:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"music"]; }
如果音乐被整个应用播放,然后把音乐,如下图所示是安全的打码。不要忘记在projectAppDelegate.h文件中声明AVAudioPlayer * theAudio。
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
theAudio.delegate = self;
theAudio.numberOfLoops = -1;
[theAudio play];
[[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; }
我可能在这里错过了几位。只需运行一些测试,使其成为您喜欢的工作方式。我只是尝试了我的应用程序,它为我工作。
'theAudio'是什么? – 2012-07-10 11:29:47