AVAudioPlayer produces lag despite prepareToPlay() in Swift
I ran into this same problem and played the sound in the backgroundQueue.
This is a good example: https://stackoverflow.com/a/25070476/586204.
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
audioPlayer.play()
})
Slow start for AVAudioPlayer the first time a sound is played
The delay seems to be related to instantiating AVAudioPlayer for the first time. If I load any audio, run [audioPlayer prepareToPlay] and then immediately release it, the load times for all of my other audio is very close to imperceptible. So now I'm doing that in applicationDidFinishLaunching and everything else runs well.
I can't find anything about this in the docs, but it certainly seems to be the case.
Playing instance of AVAudioPlayer from background thread sometimes fails
There is no reason to say play
on a background thread; playing a sound does not block the main thread. Moreover there is no reason to suppose that AVAudioPlayer is thread safe. And YOUR code is certainly not thread safe, as you are talking to the audio player simultaneously from different threads. So you’re using threads incorrectly. There’s no point discussing wrong behavior results. Take away your background thread and try your code doing things the right way. If there is still a problem we can talk about that.
Sound causing game to lag in swift sprite kit game?
You are getting lag because you are not preloading the sound files. You can preload them at App Launch, and then when you need just play them. For reference look into this stackoverflow's post
And if you still face the same issue then you can add sound in background queue, as demostrated here
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
audioPlayer.play()
})
AVAudioPlayer causes my game to lag
I figured it out. I used System Sound Services instead of AVAudioPlayer, and now it works perfectly.
This was very helpful: https://developer.apple.com/library/mac/documentation/AudioToolbox/Reference/SystemSoundServicesReference/index.html
Related Topics
How to Do Transforms on a Calayer
How to Integrate Nsurlconnection with Uiprogressview
"Could Not Find Any Information for Class Named Viewcontroller"
Should I Git Ignore Xcodeproject/Project.Pbxproj File
Generating an Unsigned IPA iOS Application
Swift - How to Get Last Taken 3 Photos from Photo Library
Behavior Differences Between Performblock: and Performblockandwait:
Error Appstore Connect:Missing Purpose String in Info.Plist File
Uipopovercontroller Dealloc Reached While Popover Is Still Visible
How to Make Both Header and Footer in Collection View with Swift
Does Non-Renewing Subscription Requires a Restore Button
How to Embed a Uitableview in a Uiscrollview
Using Collectionview in Uiview with Xib File
How to Set an Title of the Currently Playing Audio in iPhone Lock Screen