5
|
1 //
|
|
2 // PlaySound.swift
|
|
3 // GeoQuiz
|
|
4 //
|
|
5 // Created by Dennis Concepción Martín on 22/9/22.
|
|
6 //
|
|
7
|
|
8 import Foundation
|
|
9 import AVFAudio
|
|
10 import UIKit
|
|
11
|
|
12 class Sound {
|
|
13 private var player: AVAudioPlayer?
|
|
14
|
|
15 func play(_ filename: String) {
|
|
16 guard let soundFileURL = Bundle.main.url(forResource: filename, withExtension: "wav") else {
|
|
17 fatalError("Sound file \(filename) couldn't be found")
|
|
18 }
|
|
19
|
|
20 do {
|
|
21 try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.ambient)
|
|
22 try AVAudioSession.sharedInstance().setActive(true)
|
|
23 } catch {
|
|
24 fatalError("Couldn't activate session")
|
|
25 }
|
|
26
|
|
27 do {
|
|
28 player = try AVAudioPlayer(contentsOf: soundFileURL)
|
|
29 player?.play()
|
|
30 } catch {
|
|
31 fatalError("Couldn't play sound effect")
|
|
32 }
|
|
33 }
|
|
34 }
|
|
35
|