Files
SwiftUI-Note/第六章 水果机游戏/源代码/FruitMachine/FruitMachine/Helpers/PlaySound.swift
2021-10-17 13:32:45 +08:00

22 lines
461 B
Swift

//
// PlaySound.swift
// FruitMachine
//
// Created by on 2021/3/19.
//
import AVFoundation
var audioPlayer: AVAudioPlayer?
func playSound(sound: String, type: String) {
if let path = Bundle.main.path(forResource: sound, ofType: type) {
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer?.play()
} catch {
print("错误:无法找到文件并播放该声效文件!")
}
}
}