如何在项目中添加声音

首先我们要在添加系统关于声音的框架AVFoundation,根据下图

然后倒入我们需要用到的地方

        //创建一个统一资源定位符url,确定文件路径
        NSURL *url=[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"bg" ofType:@"mp3"]];
        //创建一个播放器
        AVAudioPlayer *player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
        player.volume=5;//播放音量
        player.numberOfLoops=-1;//播放次数,-1代表无限循环播放
        [player play];//开始播放

这样基本上就完成了声音的使用

原文地址:https://www.cnblogs.com/amoy888/p/3394859.html