2017-10-08 108 views
-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() 
    } 
} 

我需要在我的代碼或我模擬器的設置變化?

+3

我們網站上的質量過濾器是有原因的。不要試圖通過增加噪音來繞過它們。只需退後一步,重新閱讀[幫助]和[編輯]即可。 – rene

回答

0

有類似的問題,你必須去系統偏好設置>聲音>聲音效果,然後取消選中並重新檢查「播放用戶界面的聲音效果」。

0

其實發現我所要做的只是刪除文件並重新添加它。我不需要將文件拖入Xcode,而需要通過右擊並添加文件來添加文件。