【问题标题】:Loop background music? [closed]循环背景音乐? [关闭]
【发布时间】:2011-04-07 13:51:48
【问题描述】:
  1. 为我的 iOS 游戏播放背景音乐的最佳方式是什么?我该怎么做?

  2. 播放该音乐文件的最佳格式是什么?

  3. 这首歌会愉快地播放整个游戏,在切换视图时不会中断吗?

  4. 如何循环播放这首音乐?

【问题讨论】:

    标签: ios objective-c loops audio


    【解决方案1】:

    您可以使用AVAudioPlayer:

    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"mySound" ofType:@"mp3"];
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
    player.numberOfLoops = -1; //infinite
    
    [player play];
    

    你可以通过设置currentTime来跳过:

    player.currentTime = 10; //jump to 10th second
    

    【讨论】:

    • 不要忘记将AudioToolboxAVFoundation 框架添加到您的项目中,并在您的视图控制器委托文件中导入<AudioToolbox/AudioToolbox.h><AVFoundation/AVFoundation.h>
    • 传统上我发现 MP3 文件循环不好——现在不是这样了吗?
    • @Hithere - 你不需要添加AudioToolbox,这是一个不同的框架。还有@DavidDunham - 是的,AAC 文件应该比 MP3 更适合循环播放。
    • 只是一个注释, AVAudioPlayer 必须是将播放音乐的类的属性。它不能在方法内部定义。
    • 但是您可以将音频播放器放在不同的类中,并使用该类播放音乐。
    猜你喜欢
    • 2011-05-30
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2013-04-23
    相关资源
    最近更新 更多