parent
ebab036a30
commit
6aa80c9259
@ -0,0 +1,8 @@
|
||||
//
|
||||
// SpriteMeeple.swift
|
||||
// DouShouQi_App
|
||||
//
|
||||
// Created by etudiant on 27/05/2024.
|
||||
//
|
||||
|
||||
import Foundation
|
@ -0,0 +1,33 @@
|
||||
//
|
||||
// MusicPlayer.swift
|
||||
// DouShouQi_App
|
||||
//
|
||||
// Created by Nathan Verdier on 27/05/2024.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import AVFoundation
|
||||
|
||||
class MusicPlayer {
|
||||
static let shared = MusicPlayer()
|
||||
var audioPlayer: AVAudioPlayer?
|
||||
|
||||
func playBackgroundMusic(music: String) {
|
||||
if let bundle = Bundle.main.path(forResource: music, ofType: "mp3") {
|
||||
let backgroundMusic = NSURL(fileURLWithPath: bundle)
|
||||
do {
|
||||
audioPlayer = try AVAudioPlayer(contentsOf: backgroundMusic as URL)
|
||||
guard let audioPlayer = audioPlayer else { return }
|
||||
audioPlayer.numberOfLoops = -1
|
||||
audioPlayer.prepareToPlay()
|
||||
audioPlayer.play()
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func stopBackgroundMusic() {
|
||||
audioPlayer?.stop()
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
//
|
||||
// SoundPlayer.swift
|
||||
// DouShouQi_App
|
||||
//
|
||||
// Created by Nathan Verdier on 27/05/2024.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import AVFoundation
|
||||
|
||||
var audioPlayer: AVAudioPlayer?
|
||||
|
||||
func playSound(named soundName: String) {
|
||||
guard let url = Bundle.main.url(forResource: soundName, withExtension: "mp3") else { return }
|
||||
|
||||
do {
|
||||
audioPlayer = try AVAudioPlayer(contentsOf: url)
|
||||
audioPlayer?.play()
|
||||
} catch let error {
|
||||
print("Error playing sound. \(error.localizedDescription)")
|
||||
}
|
||||
}
|
Loading…
Reference in new issue