iOS -- 播放本地音频文件 (Swift)

1.封装的方法,项目中如果多处使用,可以放在工具类中

 static func play(name:String,type:String) {
    
    
        let audioPath = Bundle.main.path(forResource: name, ofType: type)
        if let filePath = audioPath {
    
    
            let url = URL(fileURLWithPath: filePath)
            var soundId:SystemSoundID = 0
            AudioServicesCreateSystemSoundID(url as CFURL, &soundId)
            AudioServicesPlaySystemSound(soundId)
        }
    }

2.使用方法

UtilsTool.play(name: "welcome_start",type: "caf")

记得点赞关注呀

猜你喜欢

转载自blog.csdn.net/weixin_43259805/article/details/123199767