我想在游戏中随机播放十几首歌曲。下一首歌不应该与当前歌曲相同。
[[[SimpleAudioEngine sharedEngine] playBackgroundMusic: song];
应该有一个循环,歌曲应该是随机的,
[[NSString stringWithFormat: @"song%i.mp3", arc4random() % 20 + 1];
//20 songs starting from song1.mp3
如果在播放用户的iPod音乐时歌曲将停止播放将会很棒。但声音效果应该仍然可用:
[[SimpleAudioEngine sharedEngine] playEffect: @"aaa.caf"]
此外,当播放ipod音乐时,启动游戏,甚至不应启动游戏音乐。
如何实现这个?
答案 0 :(得分:0)
当您启动应用时,您可以像这样检查并播放或不播放自己的音乐。
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying) {
// dont play and do whatever you think should be done in this case
} else {
[[[SimpleAudioEngine sharedEngine] playBackgroundMusic: song];
}
旁注:
//Remember to preload your effects and bg music so there is not any delay
[[SimpleAudioEngine sharedEngine] preloadEffect:@"aaa.caf"];
[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"song1.mp3"];
//And to unload your effects when you wont use them anymore or receive a mem warning, or in dealloc
[[SimpleAudioEngine sharedEngine] unloadEffect:@"aaa.caf"];
<强>更新强>
在viewDidLoad中,创建一个包含所有歌曲的字符串数组,并使用以下内容对其进行随机播放:[self shuffle],对于此实现此代码:
- (void)shuffle {
for (NSInteger i = [songsArray count] - 1; i > 0; --i) [songsArray exchangeObjectAtIndex:(arc4random() % (i+1)) withObjectAtIndex:i];
}
每当您的背景音乐完成后,请使用[self shuffle]
然后使用playBackgroundMusic:[songsArray lastObject]
来处理您的混洗播放列表。