iPhone游戏中既播发背景音乐又播放特效声音的办

iPhone游戏中既播放背景音乐又播放特效声音的办

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

    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为一直循环

要导入框架 AVFoundation.framework,头文件中 #import <AVFoundation/AVFoundation.h>;做成类的话则更方便。