音频 音效播放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 

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容