ci-dessous est le code représentant un fichier pour un lecteur audio. J'essaie de l'utiliser pour jouer des sons dans mon programme. Je ne veux pas mettre une fonction AVPlayer dans chaque contrôleur de vue pour lire des sons reproductibles, donc je cherche un moyen de tout mettre en place et d'appeler la fonction spécifique (musique, clic, intro) en cas de besoinlire l'audio en utilisant un fichier swift différent
import UIKit
import AVFoundation
class player: UIViewController
{
var music:Bool = true
var sound:Bool = true
var intro:Bool = true
var soundPlayer: AVAudioPlayer?
var clickPlayer: AVAudioPlayer?
var musicPlayer : AVAudioPlayer?
{
get
{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.musicPlayer
}
set
{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.musicPlayer = newValue
}
}
func playSound(title: String, format: String)
{
if sound == true
{
guard let url = Bundle.main.url(forResource: title, withExtension: format)
else { return }
do
{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
soundPlayer = try AVAudioPlayer(contentsOf: url)
soundPlayer?.play()
}
catch let error
{
print(error.localizedDescription)
}
}
}
func playClick()
{
if sound == true
{
guard let url = Bundle.main.url(forResource: "click", withExtension: "mp3")
else { return }
do
{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
let clickPlayer = try AVAudioPlayer(contentsOf: url)
clickPlayer.play()
}
catch let error
{
print(error.localizedDescription)
}
}
}
func playMusic()
{
if music == true
{
guard let url = Bundle.main.url(forResource: "ambientMusic", withExtension: "mp3")
else { return }
do
{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
musicPlayer = try AVAudioPlayer(contentsOf: url)
musicPlayer?.play()
musicPlayer?.numberOfLoops = -1
}
catch let error
{
print(error.localizedDescription)
}
}
}
}
fichier différent:
@IBAction func play(_ sender: Any)
{
player().playClick()
performSegue(withIdentifier: "menuToIntro", sender: self)
}
Mais il ne joue rien. Il suffit de impressions connecter AQDefaultDevice (173): sauter flux d'entrée 0 0 0x0
bien, je me suis débarrassé des journaux à l'intérieur de la console oui mais cela ne résout pas le problème –