0

I am making a music timer which will stop music (playing from Spotify, Apple Music, etc.) after an entered time. Is there a way to stop that audio session or pause the audio which is being played from another app from my app?

Since I don't have a local audio player in my app, I cannot just simply stop the player (as you usually would when playing audio from your app).

Any help is appreciated!

Giorgio
  • 103
  • 1
  • 1
  • 11
  • 2
    Doesn't `AVAudioSession.sharedInstance().setActive(true)` stop anything another app is playing if you use the `.playback` category? If that doesn't work, you could always play a silent audio file... – Sweeper Apr 19 '20 at 15:52
  • 1
    Thank you! This code: try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback) try AVAudioSession.sharedInstance().setActive(true) within a do catch statement worked. – Giorgio Apr 19 '20 at 16:44

1 Answers1

0
  1. Import AVFoundation in the file which will stop the audio.

  2. Add this code once the timer is done:

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
        try AVAudioSession.sharedInstance().setActive(true)
    } catch { print(error.localizedDescription) }
    
Sweeper
  • 213,210
  • 22
  • 193
  • 313
J. Beazerg
  • 24
  • 3