Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

COFFEECATCODING


抱着猫咪 喝着咖啡 写写代码

Swift3 - AVAudioPlayer没有声音

没声音需要检查下player是否是定义的全局属性

var audioPlayer:AVAudioPlayer?

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.white
    let audioUrl = Bundle.main.url(forResource: "music", withExtension: "mp3")!
    do{
        try audioPlayer = AVAudioPlayer(contentsOf: audioUrl)
        audioPlayer?.play()
        audioPlayer?.volume = 0.8
        audioPlayer?.delegate = self

    }catch let error as NSError
    {
        print(error.localizedDescription )
    }

}

另外,如果看到类似下面这种报错

AQDefaultDevice (173): skipping input stream

可以按照如下步骤解决:

  • 菜单栏选择 Product -->Scheme-->Edit Scheme
  • 找到Arguments
  • 添加一个环境变量 OSACTIVITYMODE 值为"disable"