2017-10-08 69 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,而需要通过右击并添加文件来添加文件。