Please help me solve a sound playing problem

Please help me solve a sound playing problem. I have an app I made for counting Teslas and playing a brief sound (my voice saying “Tesla”) when image of a Tesla on the road (takes up much of the screen for ease of tapping) is tapped. Works great except for not “resuming” the audio it stopped when done playing the audio in my App.

The App displays 2 images on the iPhone screen. First is the TESLA logo, then below it is an image I found for free on PLEX. When I tap on the image, the .onTapGesture plays a brief audio file saying “Tesla” and a variable (t) increases by 1 (“t += 1”). It displays “Teslas counted so far: /(t)”

PROBLEM: I usually have the iPhone playing an audio file or music when plugged in. When I tap the image, the app stops the playing audio file or music, increases the count of “t” (variable “t” for Teslas), then plays the file of my voice saying “Tesla”. It does not continue playing the audio or music after getting the sound to play using the .onTapGesture until I either:

  1. turn off the radio and then turn it back on, or
  2. I push a button on the radio to continue playing interrupted music/audio, or
  3. I ask Siri a question. After Siri answers the question, the background audio continues playing.

The App works fine in all other regards. It just does not resume what I was listening to before I tapped on the image and it plays the sound file I expect it to play.

I’ve tried code from Google searches, GitHub, Bing (and AI creating code), but none of the solutions I’ve seen/tried fix it.

Here is the ContentView code: (there is also code in a second file “SoundsBootcamp” shown after.

import SwiftUI
import AVFAudio

class SoundManager {
    static let instance = SoundManager()
    var player: AVAudioPlayer?
    enum SoundOption: String {
        case tada
        case Tesla
        case oops
    }
    func playSound(sound: SoundOption) {
        
        guard let url = Bundle.main.url(forResource: sound.rawValue, withExtension: ".mp3") else { return }
        
        do {
            player = try AVAudioPlayer(contentsOf: url)
            player?.play()
        } catch let error {
            print("Error playing sound. \(error.localizedDescription)")
            player?.stop()
        }
    }
}
struct ContentView: View {
    @State var t = 0
    
    var body: some View {
            VStack(alignment: .center) {
            Image("TeslaLogo")
            Image("Tesla")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .onTapGesture {
                    t += 1
                    if t.isMultiple(of: 10) {
                        SoundManager.instance.playSound(sound: .tada)
                    } else {
                        SoundManager.instance.playSound(sound: .Tesla)
                    }
                }
            Text("Teslas counted so far: \(t)").padding()
            if t == 0 {
                 Spacer()
            } else {
                if t > 0 {
                    Button("Not Tesla", role: .destructive) {
                        t -= 1
                        SoundManager.instance.playSound(sound: .oops)
                        print("play oops")
                    }
                }
            }
        }
    }
}
// Preview for testing in Xcode
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Can someone show me how to continue playing the audio or music that was playing, (currently, it will not continue playing what was playing before my Tesla Counter app sound played… until I either:

  1. turn off the radio and then turn it back on, or
  2. I push a button on the radio to continue playing interrupted music/audio, or
  3. I ask Siri a question (like: Siri “what time is it?” After Siri answers the question, the background audio continues playing.

I’ve read that Apple requires “resuming” once an interruption occurs, but I don’t know how to do that.

The code also shows that for every multiple of 10 it will play a sound “tada”, which is just for fun.

There is also a button to decrease the count in case it was accidentally tapped and we did not see a Tesla (i.e., it looked like a Tesla from a distance or on first glance, but upon closer look, it was realized that it was not a Tesla).

What is happening is that your App is taking custody of the media playback so the only way to resume your music is to go back to the Music App and press play.

The same thing occurs when you have music playing and you open up the Camera App to take some video. The music will stop playing and will not resume even when you close the Camera App. You have to switch to the music App and press play.

Is there a way to make it think that Siri was asked a question and then answered it?

Apple requires that programs will resume when there is an interruption (as part of CarPlay requirements). This is what I want for my app, but I haven’t been able to find how to code that.

Or did I do some wrong programming that I can correct?

Or is there a way to programmatically have a button pressed to resume playing?