有时候在 iPhone 游戏中,既要播放背景音乐,同时又要播放比如枪的开火音效。此时您可以试试以下方法。

首先要导入框架 AVFoundation.framework,头文件中 #import <AVFoundation/AVFoundation.h>。

NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@”wav”]; //创建音乐文件路径
NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath];
AVAudioPlayer
* musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
[musicURL release];
[musicPlayer prepareToPlay];
//[musicPlayer setVolume:1]; //设置音量大小
//musicPlayer .numberOfLoops = -1;//设置音乐播放次数 -1为一直循环
posted on 2011-04-25 14:35  Penny●zheng  阅读(403)  评论(0编辑  收藏  举报