-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