1、背景音乐播放 支持mp3格式 循环播放长音乐
这种播放音乐的方式导入框架
#import <AVFoundation/AVFoundation.h>;
NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"changan" ofType:@"mp3"]; //创建音乐文件路径
NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath];
AVAudioPlayer *thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
//创建播放器
self.myBackMusic = thePlayer; //赋值给自己定义的类变量
[myBackMusic prepareToPlay];
[myBackMusic setVolume:1]; //设置音量大小
myBackMusic.numberOfLoops = -1;//设置音乐播放次数 -1为一直循环
[myBackMusic play]; //播放
2、按钮播放声音 播放短声音
需要导入框架
#import <AudioToolbox/AudioToolbox.h>
SystemSoundID soundID;
NSString *thesoundFilePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"]; //创建音乐文件路径
CFURLRef thesoundURL = (CFURLRef) [NSURL fileURLWithPath:thesoundFilePath];AudioServicesCreateSystemSoundID(thesoundURL, & soundID);
//变量SoundID与URL对应
AudioServicesPlaySystemSound( soundID); //播放SoundID声音