音频主要有两种,俗称大小音乐。短的音效,长的音乐.看大牛学习的,勿喷,毕竟小白
#import//音效(短),音频(时间长)
```
//创建soundID
```
SystemSoundID sound = 0;
sound = [_soudIDs[name] unsignedIntValue];
// 生成soundID
if (sound==0) {
//url
CFURLRef ref = (__bridge CFURLRef)([[NSBundle mainBundle]URLForResource:name withExtension:nil]);
AudioServicesCreateSystemSoundID(ref, &sound);
//save
[_soudIDs setValue:@(sound) forKey:name];
}
//play
AudioServicesPlaySystemSound(sound);
// AudioServicesPlayAlertSound(sound);//振动效果
//音乐
if (player==nil) {
//
if (musicName==nil) {
return;
}
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:musicName withExtension:nil];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:fileUrl error:nil];
// 保存到字典中
[_players setObject:player forKey:musicName];
// 准备播放,提前加载
[player prepareToPlay];
}
[player play];
网友评论