/ / AVFoundation - Hudba iOS - ios, rýchly, avfoundation

AVFoundation - Hudba iOS - ios, rýchly, avfoundation

Snažil som sa pridať hudbu do mojej aplikácie bez konkrétneho úspechu. Pokúšam sa použiť funkciu AVFoundation a kód je nasledovný:

    //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()

Nemám žiadnu chybu, ale moja aplikácia topánky a žiadna hudba hrá. Môže niekto pomôcť?

odpovede:

1 pre odpoveď č. 1

rýchly 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()
}
}
}
}
}