0

I have a set of UIButtons that plays different sounds when you click on them manually. I have another UIButton "Play" button that I want it to auto click the other buttons when I click on it.

Any help would be appreciated.

Update:

What i meant is I want to create a function that would play each buttons in different sequence. For instance, when I click on button, it plays buttons 1,2,3. Then when I click on playButton again, it plays buttons 2,5,6 etc

I believe having the sequence in an array might be easier than having it in a file.

@IBAction func notePressed(_ sender: UIButton) {

    let soundURl = Bundle.main.url(forResource: "minisound\(sender.tag)", withExtension: "wav")

    do {
        audioPlayer = try AVAudioPlayer(contentsOf: soundURl!)
    }
    catch {

        print(soundURl)

    }

   audioPlayer.play()

}

@IBAction func playButton(_ sender: Any) {

}
picciano
  • 22,341
  • 9
  • 69
  • 82
Chelseawillrecover
  • 2,596
  • 1
  • 31
  • 51
  • Call the function of those buttons when the first one is tapped. – Tamás Sengel Apr 23 '18 at 13:32
  • @the4kman What i meant is I want to create a function that would play each buttons in different sequence. For instance, when I click on button, it plays buttons 1,2,3. Then when I click on playButton again, it plays buttons 2,5,6 etc – Chelseawillrecover Apr 23 '18 at 13:53
  • [Here's how you can generate a random number.](https://stackoverflow.com/a/24098445/3151675) – Tamás Sengel Apr 23 '18 at 13:54
  • Hi Yes I know how to generate the random numbers but what I am hoping to achieve is hard-code the random numbers using array. My aim is to track what sequence was played and then allow user to play same sequence and then compare the sequence together to confirm if user played the correct sequence. I want to learn how do to achieve this myself so if you could advise me on a better approach that would be much appreciated. – Chelseawillrecover Apr 23 '18 at 14:21

1 Answers1

0

Create a for loop.

for tag in 1...'numberOfTags' {

   let soundURl = Bundle.main.url(forResource: "minisound\(tag)", withExtension: "wav")
   do {
       audioPlayer = try AVAudioPlayer(contentsOf: soundURl!)
   }
   catch {
       print(soundURl)
   }
let timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(ViewController.audioPlayer.play())), userInfo: nil, repeats: false) 

}

all inside the playButton func.

Kowboj
  • 351
  • 2
  • 14
  • Thanks for reply. What i meant is I want to create a function that would play each buttons in different sequence. For instance, when I click on button, it plays buttons 1,2,3. Then when I click on playButton again, it plays buttons 2,5,6 etc I believe having the sequence in an array might be easier than having it in a file. – Chelseawillrecover Apr 23 '18 at 13:52
  • Oh, if you want to make it random 3 notes, then yes, make an array of numbers same as sender.tag. Inside playButton shuffle them numbers, and play inside the for loop. Shuffle: https://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Kowboj Apr 23 '18 at 13:58
  • Hi Yes I know how to generate the random numbers but what I am hoping to achieve is hard-code the random numbers using array but my aim is to track what sequence was played and then allow user to play same sequence and then compare the sequence together to confirm if user played the correct sequence. – Chelseawillrecover Apr 23 '18 at 14:19
  • Yes, so: sendButton should shuffle once and append numbers to outside declared firstArray; in notePressed at the end - if secondArray.count < 3 { secondArray.append(sender.tag) } else { print("got 3 numbers! compare them with firstArray so use a different function") } – Kowboj Apr 23 '18 at 14:22
  • Thanks for the reply. If I get you, are you saying I should generate random sequence, append the played sequence to an array. Then user plays and appends to another array. then use a func to compare both arrays? – Chelseawillrecover Apr 23 '18 at 14:30
  • 1
    Yes, all done on numbers. sender.tag is a number, so shuffle through the numbers to get firstArray and play it (all inside playButton), and then append user clicked sender.tags to secondArray, finally, If secondArray = 3, then compare both and show a smily face or a sad face – Kowboj Apr 23 '18 at 14:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169599/discussion-between-chelseawillrecover-and-kowboj). – Chelseawillrecover Apr 23 '18 at 14:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169682/discussion-between-chelseawillrecover-and-kowboj). – Chelseawillrecover Apr 24 '18 at 14:35