J'ai essayé d'ajouter de la musique à mon application sans succès particulier. J'ai essayé d'utiliser AVFoundation et mon code est le suivant:
//MUSIC
var audioPlayer = AVAudioPlayer()
let audioPath = Bundle.main.path(forResource: "happyDays", ofType: "wav")
do {
try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!))
}
catch {
// process error
}
audioPlayer.play()
Je ne reçois aucune erreur mais mon application démarre et aucune musique ne joue. Quelqu'un peut-il aider?
Réponses:
1 pour la réponse № 1rapide 3
import UIKit
import AVFoundation
class ViewController: UIViewController{
let var avPlayer: AVAudioPlayer!
@IBAction func btnPlayPauseAudio(_ sender: UIButton){
if self.avPlayer != nil{
if let myAudioUrl = Bundle.main.url(forResource: "myAudioFile", withExtension: "mp3"){
if self.avPlayer.isPlaying{
self.avPlayer.pause()
}
else{
self.avPlayer = try AVAudioPlayer(contentsOf: myAudioUrl)
self.avPlayer.prepareToPlay()
self.avPlayer.play()
}
}
}
}
}