我正在开发一个 iPad/iPhone 应用程序,并且正在使用我在委托的 didFinishLaunchingWithOptions 方法中调用的这段代码播放背景音乐:
-(void) playMusic {
NSLog(@"play music!");
NSString *sound_file;
if ((sound_file = [[NSBundle mainBundle] pathForResource:@"musicfile" ofType:@"mp3"])){
NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
self.audioPlayer.delegate = self;
[self.audioPlayer setNumberOfLoops:-1]; // Infinite loop
[self.audioPlayer prepareToPlay];
[self.audioPlayer setVolume:0.2];
[self.audioPlayer play];
} else {
NSLog(@"WARN : music file not found!");
}
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}
但我发现仅此方法平均需要 0.43 秒,这是很多!我做对了吗?或者有没有更快的方法来开始音乐播放?请帮忙。
我的环境:iOS5、XCode 4.3、iPad 2.0 是我现在测试的