AudioTool.swift
import UIKit
import AVFoundation
class AudioTool: NSObject {
class func playAudio(_ audioName: String, isAlert: Bool, completion: @escaping ()->()) {
guard let url = Bundle.main.url(forResource: audioName, withExtension: nil) else { return }
let urlCF = url as CFURL
var soundID: SystemSoundID = 0
AudioServicesCreateSystemSoundID(urlCF, &soundID)
if isAlert {
AudioServicesPlayAlertSoundWithCompletion(soundID, {
AudioServicesDisposeSystemSoundID(soundID)
completion()
})
}else {
AudioServicesPlaySystemSoundWithCompletion(soundID, {
AudioServicesDisposeSystemSoundID(soundID)
completion()
})
}
}
}
使用
AudioTool.playAudio("xxx.wav", isAlert: true) {
print("播放完成")
}