The O.G. Virtual learning! Buy one, get 2 more Lifetime Memberships! - Click Here!

Play songs randomly and continuously

Home 4 Forums iOS / tvOS Starter Kits The Story Tellers Kit 2 for iOS or tvOS Apps Play songs randomly and continuously

This topic contains 4 replies, has 2 voices, and was last updated by  Justin 2 years, 11 months ago.

  • March 23, 2018 at 8:24 am #181945

    Hi Justin,

    I have 5 songs that I want to play randomly and continuously while the user is playing with the app.

    I also want to set an Audio On/Of button to play/stop the music whenever they want.

    How can I do this?

    I tried with PlaySoundFromPool but play only 1 song.

    Thanks for your help!

  • March 23, 2018 at 8:33 am #181946

    Do a search for PlaySoundFromPool and you’ll find this…

     func playSoundFromPool(_ thePool:String) {
            if let poolDict:NSDictionary = self.dict?.object(forKey: "Pools") as? NSDictionary {
                if let poolArray = poolDict.object(forKey: thePool) as? [String] {
                    let soundToPlay:String = self.pickStringFromArray(poolArray as NSArray)
                    let sound:SKAction = SKAction.playSoundFileNamed(soundToPlay, waitForCompletion: false)

    Put some extra print statements in there to see if that is failing at some point. Its a pretty simple function, so I’m wondering if thats actually the issue or something else. Get this working, and we can figure out the stop / play buttons

  • March 23, 2018 at 9:13 am #181949

    Sorry I don´t Know what do you mean with: “Put some extra print statements in there…”

    I´m absolutely no programmer 🙁

    I found the code, what I have to do Know…?

  • March 23, 2018 at 10:10 am #181954

    The sounds contained in a pool are all played one after the other or only one of them randomly?

    • March 23, 2018 at 10:12 pm #182004

      Yeah it should be random…..

      So I just noticed there already is a print statement in there that should Output the name of the file it picked at random. So if the Output window isn’t show you the name of at least one of the files in your pool array, that means there’s probably something wrong with picking it altogether. Like it couldn’t get that far in the code. Which is why I suggested adding in more print statements to basically read where the code is getting to. Even if you aren’t a code, you can insert in some print statements like…

      print("did this run")

      Just to test what’s happening. It might be as simple as a difference between the array in the Pools and the one you wrote after PlaySoundFromPool

You must be logged in to reply to this topic.

Know what an affiliate program is? You make money just by sharing links to our site! Win. Win.

Earn when you refer any buyer here! 30 day tracking. Commissions are 33%-50% and recur on subscription products!

Uh oh, missing your affiliate ID. Create one real quick