2011-08-14 65 views
0

我想在我的游戏中洗牌一打歌曲。下一首歌曲不应该与当前歌曲相同。SimpleAudioEngine洗牌播放列表

[[[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

播放列表来自我的游戏,而不是用户的iPod播放列表。 – OMGPOP

回答

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"]; 

UPDATE

在viewDidLoad中,创建您的所有歌曲的字符串数组,并与洗牌他们:自我洗牌],这个实现此代码:

- (void)shuffle { 
    for (NSInteger i = [songsArray count] - 1; i > 0; --i) [songsArray exchangeObjectAtIndex:(arc4random() % (i+1)) withObjectAtIndex:i]; 
} 

每当你的酒泉音乐完成使用[self shuffle],然后playBackgroundMusic:[songsArray lastObject]应该照顾你的洗牌播放列表。

+0

我需要的是洗牌播放列表。在完成当前歌曲后随机选择一首新歌曲播放。你的代码将只播放一首歌曲 – OMGPOP

+0

我用洗牌更新了答案。 –

+0

如何在一首歌完成播放时触发 - (无效)随机播放。 – OMGPOP