【问题标题】:SimpleAudioEngine shuffle playlistSimpleAudioEngine 随机播放列表
【发布时间】:2013-03-17 03:03:35
【问题描述】:

我想在我的游戏中随机播放十几首歌曲。下一首歌曲不应与当前歌曲相同。

[[[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 音乐播放时,然后启动游戏,游戏音乐甚至不应该启动。

如何实现?

【问题讨论】:

  • 播放列表来自我的游戏,而不是用户的 ipod 播放列表。

标签: iphone ios audio cocos2d-iphone simpleaudioengine


【解决方案1】:

当您启动您的应用程序时,您可以像这样检查并播放或不播放自己的音乐。

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],这应该会处理您的随机播放列表。

【讨论】:

  • 我需要的是随机播放一个播放列表。播放完当前歌曲后随机选择一首新歌曲播放。您的代码将只播放一首歌曲
  • 如何在播放完一首歌曲后触发 -(void) shuffle。
  • 我对 SimpleAudioEngine 不熟悉,您可以检查是否有任何可以使用的通知。
  • NSSound 有一个名为 didFinishedPlaying 的方法,但正如我所说,我不知道 SimpleAudioEngine
  • 我只是在随机播放中添加了一个 if-else,如果没有播放音乐,则选择一首歌曲播放。然后安排您的随机播放方法。现在效果很好。
猜你喜欢
  • 2023-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-02
  • 2012-03-11
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多