AVPlayer UIButton - show play or pause image button depending on whether the stream is on or off

Hello All,

I have an app, which is playing url mp3 audio with two image buttons - play and stop. Now I would like to improve it a little bit. I have two .png images (play.png and pause.png) right now and I would like them to change with each other with a tap depending on whether the stream is on or off. Any ideas how to make it? Here is my code:

import UIKit
import AVKit
import MediaPlayer

class ViewController: UIViewController, AVAudioPlayerDelegate {
    
    var player : AVPlayer!
    var dict = NSDictionary()
    
    
    
    
    @IBAction func playButtonPressed(_ sender: UIButton){
        let url = "https://stream.com/radio.mp3"
        do {
            try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
            try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: [])
            print("Playback OK")
            try AVAudioSession.sharedInstance().setActive(true)
            print("Session is Active")
        } catch {
            print(error)
        }
        
            player = AVPlayer(url: URL(string: url)!)
            player.volume = 1.0
            player.rate = 1.0
            player.play()
        
    
    }
    
    
    
    @IBAction func stopButtonStopped(sender: UIButton) {
        player.pause()
    }

Have you IBOutlets for the buttons ?

How do you want to change ?

You have 2 buttons, : Play. Stop

  1. Do you want to swap Play and Stop ?
  2. Do you want to change their images ?

Whatever you want, you should do it in the IBActions:

  1. change their frames (assign Play frame to Stop and vice versa
  2. load new image of each

Something is not clear: why do you need buttons ?

Accepted Answer

Here is how I would do it:

  • create a property
var isPlaying = false
  • Have a single button.
  • In the IBAction, test if isPlaying

So code would be like:

class ViewController: UIViewController, AVAudioPlayerDelegate {
     
     var player : AVPlayer!
     var dict = NSDictionary()
     var isPlaying = false
     let playImage = UIImage(named: "play.png")
     let pauseImage = UIImage(named: "pause.png")

     override func viewDidLoad() {
          
          super.viewDidLoad()
     }
     
     @IBAction func buttonPressed(_ sender: UIButton){
          
          if isPlaying {
               player.pause()
               sender.setImage(pauseImage, for: .normal)
          } else {
               let url = "https://stream.com/radio.mp3"
               do {
                    try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
                    try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: [])
                    print("Playback OK")
                    try AVAudioSession.sharedInstance().setActive(true)
                    print("Session is Active")
               } catch {
                    print(error)
               }
               
               player = AVPlayer(url: URL(string: url)!)
               player.volume = 1.0
               player.rate = 1.0
               player.play()
               sender.setImage(playImage, for: .normal)
          }
          
          isPlaying.toggle()
     }
     
//     @IBAction func stopButtonStopped(sender: UIButton) {
//          player.pause()
//     }

}
AVPlayer UIButton - show play or pause image button depending on whether the stream is on or off
 
 
Q