UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to play sound on stepper increment & decrement and also using on change modifier

Forums > SwiftUI

I have my audioPlayer setup already

In addition to the current functionality of the stepper, I want to also play separate sounds for onIncrement & onDecrement.

This project uses Core Data to persist. $estimatorData.qty listens to published var my View Model when the qty changes the new qty is saved in my view model estimatorData.save()

Here is a link to docs Stepper

I am trying to wrap my head around if one of the initializers would fit with what I am trying to accomplish

     Stepper("", value: $estimatorData.qty.onChange { qty in
                    estimatorData.save()
                },in: 0...10000)
                    .frame(width: 100, height: 35)
                    .offset(x: -4)
                    .background(colorScheme == .dark ? Color.blue : Color.blue)
                    .cornerRadius(8)

Here are my players

func incrementTaped() {
        playSound(sound: "plus", type: "mp3")
    }

 func decrementTaped() {
      playSound(sound: "minus", type: "m4a")
  }

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.