Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios AVAudioPlayer没有声音_Ios_Swift_Avaudioplayer - Fatal编程技术网

Ios AVAudioPlayer没有声音

Ios AVAudioPlayer没有声音,ios,swift,avaudioplayer,Ios,Swift,Avaudioplayer,我正在尝试将循环音乐添加到我的应用程序中。模拟器在运行,但是没有音乐出来 import AVFoundation class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } var audioPlayer = AVAudioPlayer() func startAudio() { let filePath = Bundle.main

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

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

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

不太确定,我的目标是添加一个音乐,在应用程序中我的视图/页面的每个部分循环播放。我运行了应用程序,模拟器运行时没有任何调试问题。只有音乐没有播放出来。显示的代码从不调用
startAudio()
。确保你真的这么做了。