-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlaySound.swift
37 lines (33 loc) · 1.05 KB
/
PlaySound.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//
// File.swift
// SaveTheEnvironment
//
// Created by jpcm2 on 18/04/22.
//
import Foundation
import AVFoundation
var audioPlayerbg: AVAudioPlayer = AVAudioPlayer()
var audioPlayerEffects: AVAudioPlayer = AVAudioPlayer()
func playSoundbg(sound: String, type: String){
if let path = Bundle.main.path(forResource: sound, ofType: type){
do{
audioPlayerbg = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayerbg.numberOfLoops = -1
audioPlayerbg.volume = 0.5
audioPlayerbg.play()
}catch{
print("ERROR: Could not find and play the sound file")
}
}
}
func playSoundEffects(sound: String, type: String){
if let path = Bundle.main.path(forResource: sound, ofType: type){
do{
audioPlayerEffects = try AVAudioPlayer.init(contentsOf: URL(fileURLWithPath: path))
audioPlayerEffects.volume = 0.6
audioPlayerEffects.play()
}catch{
print("ERROR: Could not find and play the sound file")
}
}
}