SimpleAudioEngine 随机播放列表

发布于 2024-11-29 14:40:12 字数 480 浏览 1 评论 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音乐正在播放时,然后启动游戏,游戏音乐甚至不应该启动。

如何实施?

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];
//20 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断舍离 2024-12-06 14:40: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] ,它应该处理你的随机播放列表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文