-2
我正在寻找为什么我的声音不会在iOS模拟器中播放的原因。按下按钮后,没有声音在iOS模拟器中播放
程序运行,但当我按下按钮没有声音播放。
这是我的代码:
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player:AVAudioPlayer?
func playSound() {
if let soundURL = Bundle.main.url(forResource: "sound", withExtension: "mp3") {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(contentsOf: soundURL)
if let thePlayer = player {
thePlayer.prepareToPlay()
thePlayer.play()
}
}
catch {
print(error)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Button(_ sender: Any) {
playSound()
}
}
我需要在我的代码或我模拟器的设置变化?
我们网站上的质量过滤器是有原因的。不要试图通过增加噪音来绕过它们。只需退后一步,重新阅读[帮助]和[编辑]即可。 – rene