音频 音效播放AVFoundation

音频主要有两种,俗称大小音乐。短的音效,长的音乐.看大牛学习的,勿喷,毕竟小白


#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];

下载使用 https://github.com/dong1993/avfoundation 

你可能感兴趣的:(音频 音效播放AVFoundation)