How can I allow background music to continue playing while my app still plays its sounds while using Swift

Nathan Harper

I created an app and I am attempting to allow the user to continue to listen to their music while playing my game, but whenever they hit "play" and the ingame sounds occur it will stop the background music. I am developing on iOS using Swift. Here is a piece of the code that initiates the ingame sounds.

func playSpawnedDot() {
    var alertSound: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("spawnDot", ofType: "mp3")!)!
    var error:NSError?
    audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
    audioPlayer.prepareToPlay()

    if volumeBool {
        audioPlayer.play()
    }
}
lchamp

You need to set the AVAudioSession category, with one of the following value: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/index.html (AVAudioSession Class Reference).

The default value is set to AVAudioSessionCategorySoloAmbient. As you can read :

[...] using this category implies that your app’s audio is nonmixable—activating your session will interrupt any other audio sessions which are also nonmixable. To allow mixing, use the AVAudioSessionCategoryAmbient category instead.

You have to change the category, before you play your sound. To do so :

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)

You don't need to call those line each time you play the sound. You might want to do it only once.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to continue playing music when I killed App

分類Dev

ionic 2 sounds stop background music while loading audio

分類Dev

How can i make my springboot app websocket app get connected via a "ws://" url while using stomp

分類Dev

While my app is running, how can I update the current activity with the contents a GCM notification, without clicking on the notification?

分類Dev

How can I edit my code while debugging using Visual Studio?

分類Dev

How to run Gunicorn while still using WebSocket

分類Dev

How can i add music settings for my app?? Like Android games

分類Dev

How to make a objective-c written iOS app containing a swift-written app extension while still supporting iOS 6?

分類Dev

How can I integrate complexity verification while using a PasswordBox?

分類Dev

How can I retrieve HttpServletRequest data while using Jersey

分類Dev

How can I make my app behave like I'm changing Application is agent(UIElement) at runtime using Swift?

分類Dev

How can I convert a folder of .pngs to .jpgs while setting the background color?

分類Dev

These spaces are appearing to me suddenly while I'm writing my code, how can I remove them?

分類Dev

How to configure Background App Refresh using Swift?

分類Dev

How can I add a video to my website that plays when you click a button?

分類Dev

how do I ssh while automatically using the password I use for my account

分類Dev

I need my array to return and array back to another variable while also including its previous array members, no idea how to go about it

分類Dev

Is there an advantage to using a continue block with a while loop in Perl?

分類Dev

How can I use fetch in while loop

分類Dev

How can i minimize the side bar in my website while opening another page?

分類Dev

How can I display the user's live wallpaper as background in my app?

分類Dev

If I have location services turned on then how can my app get killed when it enters background mode?

分類Dev

how to make my slider continue its loop?

分類Dev

How I can restrict position view while moving/dragging on PDFView? (Swift)

分類Dev

how can i show UIActivityIndicator while the app receive data from Firebase Realtime database

分類Dev

While i am running the code below i am getting the adapter error,i tried solving it by seeing yt videos ....still its not working

分類Dev

How do I serve my index.html file while using PHP Slim REST Api?

分類Dev

How i can get the current selected fragment id in navigation graph while i'm using two graphs

分類Dev

Can Windows run on my computer while I am running Ubuntu?

Related 関連記事

  1. 1

    How to continue playing music when I killed App

  2. 2

    ionic 2 sounds stop background music while loading audio

  3. 3

    How can i make my springboot app websocket app get connected via a "ws://" url while using stomp

  4. 4

    While my app is running, how can I update the current activity with the contents a GCM notification, without clicking on the notification?

  5. 5

    How can I edit my code while debugging using Visual Studio?

  6. 6

    How to run Gunicorn while still using WebSocket

  7. 7

    How can i add music settings for my app?? Like Android games

  8. 8

    How to make a objective-c written iOS app containing a swift-written app extension while still supporting iOS 6?

  9. 9

    How can I integrate complexity verification while using a PasswordBox?

  10. 10

    How can I retrieve HttpServletRequest data while using Jersey

  11. 11

    How can I make my app behave like I'm changing Application is agent(UIElement) at runtime using Swift?

  12. 12

    How can I convert a folder of .pngs to .jpgs while setting the background color?

  13. 13

    These spaces are appearing to me suddenly while I'm writing my code, how can I remove them?

  14. 14

    How to configure Background App Refresh using Swift?

  15. 15

    How can I add a video to my website that plays when you click a button?

  16. 16

    how do I ssh while automatically using the password I use for my account

  17. 17

    I need my array to return and array back to another variable while also including its previous array members, no idea how to go about it

  18. 18

    Is there an advantage to using a continue block with a while loop in Perl?

  19. 19

    How can I use fetch in while loop

  20. 20

    How can i minimize the side bar in my website while opening another page?

  21. 21

    How can I display the user's live wallpaper as background in my app?

  22. 22

    If I have location services turned on then how can my app get killed when it enters background mode?

  23. 23

    how to make my slider continue its loop?

  24. 24

    How I can restrict position view while moving/dragging on PDFView? (Swift)

  25. 25

    how can i show UIActivityIndicator while the app receive data from Firebase Realtime database

  26. 26

    While i am running the code below i am getting the adapter error,i tried solving it by seeing yt videos ....still its not working

  27. 27

    How do I serve my index.html file while using PHP Slim REST Api?

  28. 28

    How i can get the current selected fragment id in navigation graph while i'm using two graphs

  29. 29

    Can Windows run on my computer while I am running Ubuntu?

ホットタグ

アーカイブ