当我在Xcode中测试我的Swift应用程序时,偶尔会出现此错误:
22:16:49.556错误:181:在0.012秒(1464 1465)之后超时; mMajorChangePending = 0
我不知道这意味着什么,因为它很少发生,似乎没有引起任何问题。我环顾四周,似乎它可能与我的AVAudioPlayers有关,但我看不出我做错了什么,也不知道他们是如何受到影响的,因为我的声音很好。可能是造成此错误的原因是什么?
这是我创建音频播放器的地方
//Player arrays hold multiple players so sounds can be layered on top of each other instead of cutting each other off
var correctPlayerArray:[AVAudioPlayer] = [] //Plays sound when user hits red dot
var wrongPlayerArray:[AVAudioPlayer] = [] //Plays sound when user hits green dot
override func viewDidLoad()
{
super.viewDidLoad()
GradientBackground.createGradientLayer(self.view, index: 0)
view.removeConstraints(view.constraints)
newView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(newView)
menuView.hidden = true
menuViewPlayAgain.enabled = false
let widthConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0)
let heightConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0)
let xConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
let yConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
view.addConstraint(widthConstraint)
view.addConstraint(heightConstraint)
view.addConstraint(xConstraint)
view.addConstraint(yConstraint)
print("New button array size: \(buttonArray.count)")
//Adds notifications to indicate when app has been closed
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.exitApp), name: UIApplicationWillResignActiveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.exitApp), name: UIApplicationWillTerminateNotification, object: nil)
//Adds notifications to indicate when app has been reopened
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.loadApp), name: UIApplicationDidBecomeActiveNotification, object: nil)
//Instantiates correct player and wrong player
let correctPath = NSBundle.mainBundle().pathForResource("blopSound", ofType: "wav")!
let wrongPath = NSBundle.mainBundle().pathForResource("punchSound", ofType: "wav")!
do
{
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
try AVAudioSession.sharedInstance().setActive(true)
for _ in 0..<10
{
//var correctPlayer:AVAudioPlayer = AVAudioPlayer()
let correctPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: correctPath))
correctPlayer.volume = soundsVolume
correctPlayer.prepareToPlay()
self.correctPlayerArray.append(correctPlayer)
}
for _ in 0..<10
{
//var wrongPlayer:AVAudioPlayer = AVAudioPlayer()
let wrongPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: wrongPath))
wrongPlayer.volume = soundsVolume
wrongPlayer.prepareToPlay()
self.wrongPlayerArray.append(wrongPlayer)
}
}
catch
{
print("Error: Couldn't create audio players")
}
startTimer()
}
这是我播放声音的地方
func playSound(soundName:String)
{
if soundsOn
{
switch soundName
{
case "Correct":
print("Correct")
playSoundFromArray(correctPlayerArray)
case "Wrong":
print("Wrong")
playSoundFromArray(wrongPlayerArray)
default:
print("Nothing")
}
}
}
func resetAudioPlayer(player:AVAudioPlayer)
{
player.pause()
player.currentTime = 0
player.prepareToPlay()
}
func playSoundFromArray(soundArray:[AVAudioPlayer])
{
var playedSound = false
var i = 0
while i < soundArray.count && playedSound == false
{
if !soundArray[i].playing
{
soundArray[i].play()
playedSound = true
}
i += 1
}
}
每次按下按钮时都会播放声音,由于屏幕上同时有大约15个按钮,所有这些按钮都应该在几毫秒之内按下,我创建了一堆音频播放器以便在声音播放时彼此相对。