开发者

SimpleAudioEngine shuffle playlist

开发者 https://www.devze.com 2023-03-28 04:08 出处:网络
i want to shuffle a dozen of songs in my game. Next song should not be the same as current song. [[[SimpleAudioEngine sharedEngine] playBackgroundMusic: song];

i want to shuffle a dozen of songs in my game. Next song should not be the same as current song.

[[[SimpleAudioEngine sharedEngine] playBackgroundMusic: song];

There should be a loop and song should be randomized,

[[NSString stringWithFormat: @"song%i.mp3", arc4random() % 20 + 1];
//2开发者_开发技巧0 songs starting from song1.mp3

It would be great if the song will stop playing when the user's ipod music is playing. But the sound effect should still be available:

[[SimpleAudioEngine sharedEngine] playEffect: @"aaa.caf"]

Also, when the ipod music is playing, then launch the game, game music should not even be started.

How to implement this?


When you start your app, you can check like this and play or not your own music.

if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying) {
    // dont play and do whatever you think should be done in this case
} else {
    [[[SimpleAudioEngine sharedEngine] playBackgroundMusic: song];
}

Side note:

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

In viewDidLoad, create an strings array with all your songs and shuffle them with: [self shuffle],for this implement this code:

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

Every time your background music finishes use [self shuffle] and then playBackgroundMusic:[songsArray lastObject] that should take care of your shuffled playlist.

0

精彩评论

暂无评论...
验证码 换一张
取 消