我正在使用iPad / iPhone App&我正在播放这段代码片段的背景音乐,我在Delegate的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是我现在用它测试的