0

ゲーム内で数十曲をシャッフルしたい。次の曲は現在の曲と同じであってはなりません。

[[[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の音楽を再生しているときにゲームを起動すると、ゲームの音楽も開始されません。

これを実装する方法は?

4

1 に答える 1

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]、シャッフルされたプレイリストが処理されます。

于 2011-08-14T06:14:11.943 に答える