循环背景音乐?

Jos*_*ane 23 audio loops objective-c ios

  1. 为iOS游戏播放背景音乐的最佳方式是什么?我该怎么做?

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

  3. 这首歌是否会在整个游戏中愉快地播放,改变观看时不会中断?

  4. 我怎么能循环这个音乐?

小智 36

你可以使用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];
Run Code Online (Sandbox Code Playgroud)

你可以通过设置currentTime:

player.currentTime = 10; //jump to 10th second
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记将`AudioToolbox`和`AVFoundation`框架添加到项目中,并在视图控制器委托文件中导入`<AudioToolbox/AudioToolbox.h>`和`<AVFoundation/AVFoundation.h>`. (13认同)
  • 只需注释,AVAudioPlayer必须是播放音乐的类的属性.它不能在方法内定义. (4认同)
  • @Hithere - 你不需要添加`AudioToolbox`,这是一个不同的框架.还有@DavidDunham - 是的,AAC文件应该比用于循环的MP3更好. (3认同)