//soundFileName为要播放的音乐名称(不带后缀.mp3)
NSString *fileName = [[NSBundle mainBundle]pathForResource:soundFileName ofType:@"mp3"];
//是否能找到该音乐文件
if([[NSFileManager defaultManager]fileExistsAtPath:fileName])
{
NSURL *fileUrl = [NSURL fileURLWithPath:fileName];
NSError *err = nil;
player = [[AVAudioPlayer alloc]initWithContentsOfURL:fileUrl error:&err];
if(!player)
{
//问题处理
}
else
{
[player setNumberOfLoops:-1];
[player setDelegate:self];
// 调节音量 (范围从0到1)
player.volume = 0.5;
// 准备buffer,减少播放延时的时间
[player prepareToPlay];
[player play];
}
}
else
{
NSLog(@"没有找到文件");
}