gpt4 book ai didi

ios - 重新编辑: Audio not playing in real ios Device but playing in simulated devices in Swift2

转载 作者:行者123 更新时间:2023-11-30 14:10:56 25 4
gpt4 key购买 nike

这是我在 swift2 中的代码,绝对没有错误,我在真实设备上运行但没有声音?没有异常(exception),没有什么我错过了什么?

  import UIKit
import AVFoundation

class ViewController: UIViewController {


@IBAction func play() {
audioPlayer.play()
}

var audioPlayer : AVAudioPlayer!



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.


let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("drum", ofType: "mp3")!)

do {
audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound)
audioPlayer.prepareToPlay()
audioPlayer.volume = 1.0
audioPlayer.play()
print("yes")

}catch _ {
audioPlayer = nil
}
}
}

最佳答案

我从 Apple WWDC 大会的一个视频中发现了问题所在

http://adcdownload.apple.com/videos/wwdc_2010__hd/session_412__audio_development_for_iphone_os_part_1.mov (仅在 Safari 上可见)

问题是音频路由,当我插入耳机时,我可以听到真实设备的声音。当路由更改时,我们需要使用 AVAudioSession 设置正确的音频路由。更多信息请参见上面的视频。

关于ios - 重新编辑: Audio not playing in real ios Device but playing in simulated devices in Swift2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31773093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com