-1

New to the community and coding, but just thought I would ask for some pointers.

Wanting to build an mobile app the essentially plays a sound after a button press in the app. Ideally the sound would be played after the "random" amount of time which I hope to keep from 5-15 seconds.

Not looking for someone to make this, just want some pointers in the right direction!

1 Answers1

1

There are many tutorials on how to create a basic UI with a storyboard and connect your button to an action in your code. You could perhaps start with this one.

You can check out the arc4random_uniform function to generate random numbers, for 5 - 15 it would look something like:

let randomValue = arc4random_uniform(11) + 5

to do something after a period of time, the easiest way is probably using GCD via dispatch_after:

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(NSTimeInterval(randomValue) * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
    // Play a sound
}

As for playing a sound, here's a question about that very topic.

Community
  • 1
  • 1
Charles A.
  • 10,685
  • 1
  • 42
  • 39