Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

A simple fix for audio backgrounding and playing sound effects in SpriteKit apps.

License

Notifications You must be signed in to change notification settings

george-lim/spritekit-bgm-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

SpriteKit Background Music Guide
GitHub downloads GitHub release GitHub issues GitHub pull requests license

While working with Apple's SpriteKit framework, I came across an annoying problem which took way too much time to solve - being unable to preserve music from an external app coming into my app, and still play sound effects. This repo contains the simple fix to the problem and an example app to clearly demonstrate the fix.

  1. The Solution
  2. The Problem Explained

The Solution

For those who just want the solution (SAVAGES), here it is. Simply paste the following into your root view controller's viewDidLoad() function.

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
    try AVAudioSession.sharedInstance().setActive(true)
} catch let error {
    print(error.localizedDescription)
}

Now, you are free to use the SKAction.playSoundFileNamed(soundFile: String, waitForCompletion: Bool) action in all of your SKScenes to play sound effects without disturbing the music / external audio source in the background.

The Problem Explained

For many people using SpriteKit, they may not know that you can still use SKAction's playSoundFileNamed function after setting the AVAudioSession category to Ambient. In SpriteKit, SKAction.playSoundFileNamed(...) is the easiest way to play a sound effect, so many people opt to use the function over AVAudioPlayer.

A few advantages to using SKAction to play sound effects instead of AVAudioPlayer include:

  1. You do not need to call prepareToPlay() in order to play a simple sound, nor will you experience any delays in playing the sound after the action is ran.
  2. You do not need to worry about managing multiple instances of AVAudioPlayer if you want to have the same sound effect played multiple times before the first sound effect finishes.
  3. WAY more optimized for game sound effects than AVAudioPlayer.

I was under the (incorrect) impression that I had to use AVAudioPlayer in my app to play sound effects in order to keep my music in the background, but this turns out to be false. I tried a neat framework called SwiftySound at one point to play sounds, but when the same sound effect is called > ten times a second for a game, the game begins lagging due to storing ten references of AVAudioPlayer.

And before I get shot for putting this simple learning experience on GitHub, here is a list of Stack Overflow questions I came across trying to solve the problem I had which DID NOT HELP.

  1. questions/24043904/creating-and-playing-a-sound-in-swift
  2. questions/28996589/swift-spritekit-playing-audio-in-the-background
  3. questions/32312827/in-swift-how-do-i-let-other-apps-continue-to-play-audio-when-my-app-is-open
  4. questions/43929090/play-continuous-short-sound-without-lagging-ui-animation
  5. questions/25503629/avaudioplayer-produces-lag-despite-preparetoplay-in-swift
  6. questions/42462049/avaudioplayer-produces-lag-playing-sound-effect
  7. questions/34680007/simple-low-latency-audio-playback-in-ios-swift
  8. questions/31792872/play-sound-effect-without-latency
  9. questions/26998764/ios-swift-play-background-music-and-sound-effects-without-delay
  10. questions/900461/slow-start-for-avaudioplayer-the-first-time-a-sound-is-played/3082176#3082176
  11. questions/5148348/avaudioplayer-lag-when-calling-play
  12. questions/14162805/playing-sound-effect-with-no-latency-in-objective-c
  13. questions/36865233/get-avaudioplayer-to-play-multiple-sounds-at-a-time
  14. questions/16681668/how-to-have-multiple-audio-streams-play-overlapping-in-ios
  15. questions/28286767/openal-vs-avaudioplayer-vs-other-techniques-for-playing-sounds
  16. questions/9970817/play-overlapping-sound-in-ios
  17. questions/3128283/what-is-the-best-way-to-play-sound-quickly-upon-fast-button-presses-xcode?rq=1

About

A simple fix for audio backgrounding and playing sound effects in SpriteKit apps.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages