アプリを起動すると、このようにチェックして、自分の音楽を再生するかどうかを確認できます。
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]
、シャッフルされたプレイリストが処理されます。