每次打开应用程序时,我都尝试播放一次循环声音文件。我这样做是因为我目前在初始 viewController 的 viewDidLoad 中有代码。问题是每次我切换视图然后回到初始视图控制器时,它都会再次播放声音文件,声音会重叠,听起来很糟糕。我已经在下面发布了我的代码。非常感谢任何帮助,在此先感谢!
{
if (![[NSUserDefaults standardUserDefaults]
boolForKey:@"loadSongOnce"]) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Cartoon Sound" ofType:@"mp3"];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.numberOfLoops = -1;
[theAudio play];
[[NSUserDefaults standardUserDefaults] setBool:YES
forKey:@"loadSongOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}