2

In my app, for a button click or any tap I need to play one beep sound on user click or tap. I added this code in AppDelegate:

var audioPlayer: AVAudioPlayer?
func playSound() {
        let audioFileURL = Bundle.main.url(forResource: "/Sound/music1", withExtension: "mp3")
        do {
            try audioPlayer = AVAudioPlayer(contentsOf: audioFileURL!)
        } catch let error {
            print(error.localizedDescription)
        }
        audioPlayer?.play()
    }

I don't know how to merge or pass these methods to all of my taps and button clicks to play that beep sound. I don't want to add in each button click in each vc. Any help with dynamic solutions for the button click and any touch event in my app?

Jerry D
  • 381
  • 11
  • 25
tivin does
  • 23
  • 5
  • You want your entire screen as one button? so when you touch the screen it makes a beep? – Asbis Mar 21 '19 at 19:39
  • noo....in my app i have many button and navigation bar back button and click events. So when ever any touch action happens,i needs to play that beeb sound with those actions – tivin does Mar 21 '19 at 19:42
  • So you want every buttons in your view to make the same beep whenever they touch a button? – Asbis Mar 21 '19 at 19:49
  • yes, And also i have some collection view cell. So if i tap also i need to play that touch sound – tivin does Mar 21 '19 at 19:51
  • @Asbis any possibilities ? – tivin does Mar 21 '19 at 20:49
  • I will help you tomorrow. – Asbis Mar 21 '19 at 20:52
  • @Asbis Any help on that..i tried all the thing...nothing is worked – tivin does Mar 22 '19 at 07:13
  • Could you post a screenshot of your app or a sketch of what you want it to look like? – Asbis Mar 22 '19 at 07:37
  • why screenshot needs ?. I added that posted code in appdelegate. So when ever user tap in my app i need to beep the sound... – tivin does Mar 22 '19 at 07:38
  • oh! Now i understand what you are trying to achieve. Have you seen this? https://stackoverflow.com/questions/37996497/swift-how-to-play-sound-when-screen-is-touched – Asbis Mar 22 '19 at 07:43
  • @Asbis now i will try that...but can u please help me here..its bit urgent :https://stackoverflow.com/questions/55294818/reason-firtimestamp-encodewithcoder-crash – tivin does Mar 22 '19 at 07:47

1 Answers1

0

Well you can call the function playSound() in every action for the buttons if you need diferentes gestures, like a long press you can use a Gesture Recognizer.

Example if you need play a simple beep when the button has pressed, just call playSound() inside the @IBAction of the button, but you need play a error beep if the user press the button for long period of time, so add a UILongPressGestureRecognizer in your buttons like this: enter image description here

So you can link a gesture Recognizer to you code like a simple @IBAction like this:

enter image description here

But this is just you need diferentes gestures, if you need a simple beep why you no try this:

@IBAction func testAction(_ sender: UIButton) {
     playSound()
}
Humberto Lima
  • 46
  • 1
  • 3
  • thanks for your solution, but please read my post. i asked for dynamic in one place. i no needs to write in all ibaction. – tivin does Mar 21 '19 at 20:13
  • Well, I can not understand why you need it dynamically. but I did a simple example, and posted in my git look whether it is the answer, or if at least it's a start, this is the link: https://github.com/TexugoProgramador/tapTest – Humberto Lima Mar 21 '19 at 23:59
  • You save a position and width / height of buttons in array, so using the location off touch you make a loop in array to compare touch position with buttons position – Humberto Lima Mar 22 '19 at 00:01
  • dynamically means. In my appdelegate i needs to write that code.And when ever any button click happens i need to call that. like setting nav color default in app delegate for all vc in whole app – tivin does Mar 22 '19 at 04:27
  • you can make a custom Button class? like this: class customButton: UIButton { func playSound() { print("sound played") } override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) { playSound() super.sendAction(action, to: target, for: event) } } So you just need change the classe UIButton to your customButton – Humberto Lima Mar 22 '19 at 12:11