我想在我的游戏里洗一打歌。下一首歌曲不应与当前歌曲相同。
[[[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播放音乐时,然后启动游戏,游戏音乐甚至不应该被启动。
如何实现这个目标?
发布于 2011-08-14 14:14:12
当你启动你的应用程序时,你可以像这样检查并播放或不播放你自己的音乐。
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]
,这应该会处理你混乱的播放列表。
https://stackoverflow.com/questions/7055156
复制相似问题