AVAudioPlayer-音乐无法播放

时间:2020-06-22 16:44:38

标签: avaudioplayer

我有问题。按下按钮不会播放音乐。

import UIKit
import AVFoundation

class ViewController: UIViewController {

   var player = AVAudioPlayer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        do {
            if let audioPath = Bundle.main.path(forResource: "Damage", ofType: "mp3") {
            try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath))
            }
        } catch  {
            print("Error")
        }
        
        self.player.play()
    }
    
    @IBAction func playButton(_ sender: Any) {
        self.player.play()
    }
    
    @IBAction func pauseButton(_ sender: Any) {
        self.player.pause()
    }
    
}

0 个答案:

没有答案