SwiftUI in Xcode

Hey guys I have a few questions relating to SwiftUI in Xcode

  1. how do I restart a timer when it finishes in SwiftUI
    When the timer finishes, it just remains at 0 and does not reset back to 60 seconds (the time originally set) does anyone know how to fix this

  2. how to code it in SwiftUI so that as soon as the app opens music can start playing?
    I am creating this game in SwiftUI and I want it to be that as soon as the user opens the app on their device, music should start playing. Any ideas on how to do this ??

  3. I put a timer in my code and when there are 12 seconds left I am playing this background music to alert the user they have 12 seconds remaining. However, when they click a button, the song immediately stops so I am not sure how to make this song play in the background for the 12 seconds without stopping irrespective of what the user does.

Any help on any of the above questions would be much appreciated
Thanks
Rohan

to add sounds, same logic in swiftUI

you can use

DispatchQueue.main.async {
  sound.play()
}

so your sound will run in the background

I tried this but when I click a button on the app the music stops. This problem has actually been killing me and I don’t know how to fix it

Thanks for the suggestion though
Rohan

even if you put the play sound inside the dispatchqueue?

yeah, even if I put the sound function inside the dispatchqueue the sound still stops if a user presses a button

can you show your codes? for playing the sound, and for the pressing of buttons

From the 2 images attached:
I created a function to play the song for the last 12 seconds of the timer but as soon as the button “Hit Me!” is tapped, the song stops playing

If you require more screenshots then I can send

Also, if needed, I can attach my Xcode project for you if it makes it any easier

Thanks
Rohan

@rohan007

It would help if you included more of the body code of your App.

One thing to keep in mind with SwiftUI is that if you click a button or change a @State variable in your code, SwiftUI will redraw the screen. That’s the nature of the declarative framework.

In answer to Question 1, you might need to have a Boolean to store the fact that the game is in progress and when the timer gets to zero set that Boolean to false to indicate that the game is over. Use that to call a function to reset the timer back to whatever starting value you need.

In answer to Question 2, add the modifier .onAppear to your View and in that you can call a function to start playing your music. What you will need to have in your View is a @State boolean variable to store the fact that the music is playing so that when the view is re-drawn and .onAppear is invoked again you need have logic to prevent the music from restarting if it is already playing. Does that make sense?

In answer to Question 3, this is interrelated to Question 2 insofar as you need that boolean to store the fact that the music is playing.

Hope that helps. SwiftUI is tricky and requires a new way of thinking.

Thank you all for your help but I have encountered another problem :tired_face:

I commented the “explosion” noise and the “whoosh” noise when the user clicks on the “Hit Me!” button and “Start over” button and the background music seems to be working nicely.

The problem seems to arise when I want to play 2 different songs at the same time. This is because I want their to be background music (running in the background) and then a “whoosh”/“explosion” noise depending on what button the user presses.

Does anyone have any idea on how to play 2 different songs at the same time (without 1 song stopping whilst the other plays)

Thank you all
Rohan

Have you tried defining two separate players? One for the background music and the other for the sound effects.

Yeah I declared a separate audio players and it worked

Thanks soo much !!
Rohan

1 Like

Great news man…