AVAudioPlayer没有声音

时间:2018-01-19 15:29:46

标签: ios swift avaudioplayer

我正在尝试将循环音乐添加到我的应用中。模拟器运行,但没有音乐出现。

import AVFoundation

class ViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
  }

  var audioPlayer = AVAudioPlayer()

  func startAudio() {
    let filePath = Bundle.main.path(forResource: "backsong", ofType: "mp3")
    let fileURL = NSURL.fileURL(withPath: filePath!)
    do {
        audioPlayer = try AVAudioPlayer.init(contentsOf: fileURL, fileTypeHint: AVFileType.mp3.rawValue)
        audioPlayer.numberOfLoops = -1
        audioPlayer.volume = 1
    } catch {
        self.present(UIAlertController.init(title: "Error", message: "Error Message", preferredStyle: .alert), animated: true, completion: nil)
    }
    audioPlayer.play()
  }
}

1 个答案:

答案 0 :(得分:0)

您从未调用该函数,因此函数内的代码行不会被调用。在viewDidLoad()中调用函数startAudio()

相关问题