Title: Change play button to pause on click
Tags: ios;swift;xcode;uibutton
Question: i am building app with Xcode 11 Swift and i want to add mp3 file in my app and so far i have done this
but i want to show just one button like when user click on play button pause button should display and vice versa, how can i get this ? this is my code to play mp3 file
@IBAction func Play(_sender: Any) {
player.play()
@IBAction func Stop(_sender: Any) {
player.stop()
let audioPlayer = Bundle.main.path(forResource: "chalisa", ofType: "mp3")
try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPlayer!)) as URL

Here is the accepted answer: You can do it in one function:
// Note: you should change sender type
@IBAction func buttonPressed(_ sender: UIButton) {
if player.isPlaying {
player.stop()
sender.setImage(yourPlayImage, for: .normal)
} else {
player.play()
sender.setImage(yourPauseImage, for: .normal)
Comment for this answer: Does this change the button when it is pressed?
Comment for this answer: @koen Fixed it.
Comment for this answer: @ChandNinder What's the error? Where do you create play/pause images? Did you rename the sender type?
Comment for this answer: @ChandNinder How did you add the default button image?
Comment for this answer: @ChandNinder My code changes the button image on click. If the button has a play image, it will have a pause image. And vice versa.
You should add some code that shows how you implemented it. My code should work if you correctly linked the `buttonPressed` function with the button in Interface Builder.
Comment for this answer: how to insert play or pause image ? when i try to add play or pause image name error occured
Comment for this answer: i got it now problem is that there should be play button display for default, when i open the viewcontroller no play button shows up and when i click on the space where i have added button then pause button shows and vice versa
Comment for this answer: in viewController i have added Button that shows up as Play icon but when i click on it pause button shows up inside of Play icon and when i click on pause play button display in play button, i want to remove play button in ViewController and want to show it from code that you sent

Here is another answer: You should save the state of the player (or ask from it if you are ok with that). Then update the button based on the state:
enum PlayerState {
case playing
case stopped
mutating func toggle() {
switch self {
case .playing: self = .stopped
case .stopped: self = .playing
}
var state = PlayerState.stopped {
didSet {
switch state {
case .playing: player.play()
case .stopped: player.stop()
}
@IBAction func buttonDidTouch(_ sender: Any) { state.toggle() }

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:11/25/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><swift><xcode><uibutton>', 'Title': 'Change play button to pause on click', 'ViewCount': 1895, 'id': 59034618, 'user_AboutMe': None, 'user_AccountId': 11478702.0, 'user_DisplayName': 'Chand Ninder', 'user_Reputation': 161.0, 'user_Views': 89.0, 'user_id': 8413499.0}}