我正在使用objective开发一个iOS应用程序。当应用程序启动时,会播放背景音乐。当用户单击“帮助”按钮时,背景音乐应继续播放。另外,当用户从帮助屏幕返回到主屏幕时,背景音乐应该持续播放。
对我来说,当我从“帮助”导航到主菜单时,一种新的背景音乐将与旧的背景音乐一起播放。所以,我现在听到了两种背景音乐。
有人能帮我解决这个问题吗?
你好,巴拉蒂。
发布于 2016-06-08 17:03:57
如果您在UIApplicationDelegate中保留了对音频播放器的引用(或者其他一些保持在身边的单例),您的问题就可以得到解决。
//in the .h
@property (nonatomic, strong) AVAudioPlayer *player;
//in the .m
- (void) playMusic
{
if (self.player == nil) {
NSString *path = [NSString stringWithFormat:@"%@/music.mp3", [[NSBundle mainBundle] resourcePath]];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
}
if (!self.player.isPlaying) {
[self.player play];
}
}这样,您可以在需要的任何地方调用它:
[(MyAppDelegate*)[UIApplication sharedApplication].delegate playMusic];尽管将SoundsManager类保持为单例以处理需要跟踪的所有声音,但这可能对您有好处,但如果您需要的不仅仅是这一种,就需要跟踪这些声音。
https://stackoverflow.com/questions/37705292
复制相似问题