Reputation: 1634
Can anyone help me. I've search for my question and still can't figure out a solution, everything I found was not formatted in swift 3. Im trying to make a toggle Play and Pause out of a UIBarButtonItem. Whenever the play button is click I want it to play the file and at the same time change the UIBarButtonItem to pause and vise versa.
By the way I have embed in a navigation controller into my main view controller from the editor.
Here is my Code Snippet.
import UIKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var pausePlayBtn: UIBarButtonItem!
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
prepareMusic()
}
func prepareMusic() {
let path = Bundle.main.path(forResource: "SomeFileName", ofType: "TypeOfFile")!
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer.prepareToPlay()
} catch let err as NSError {
print(err)
}
}
@IBAction func togglePausePlay(sender: UIBarButtonItem) {
if !audioPlayer.isPlaying {
audioPlayer.play()
} else {
audioPlayer.stop()
}
}
}
I've tried the following.
First I started by creating play button and a pause button and adding them in my togglePausePlay func.
let playBtn = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: nil)
let pauseBtn = UIBarButtonItem(barButtonSystemItem: .pause, target: self, action: nil)
@IBAction func togglePausePlay(sender: UIBarButtonItem) {
if !audioPlayer.isPlaying {
audioPlayer.play()
pausePlayBtn = playBtn
} else {
audioPlayer.stop()
pausePlayBtn = pauseBtn
}
}
The second thing I tried was this.
@IBAction func togglePausePlay(sender: UIBarButtonItem) {
if !audioPlayer.isPlaying {
audioPlayer.play()
self.navigationController?.navigationBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: nil)
} else {
audioPlayer.stop()
self.navigationController?.navigationBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .pause, target: self, action: nil)
}
}
This work at the start of the app, I would click the button for the first time and everything would work the button would change to pause and file would start. However when I tried to click it again nothing happens, button stays on pause and file still keeps playing until I stop the project.
Any help would be appreciated. :)
Upvotes: 3
Views: 1951
Reputation: 152
i figured out a solution for u and its working for me.
Here's the code i tried and its working. the buttonitem is changing on its click.
import UIKit
class ViewController: UIViewController {
var playBtn = UIBarButtonItem()
var pauseBtn = UIBarButtonItem()
@IBOutlet var labelMusicState: UILabel!
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.playBtn = UIBarButtonItem(barButtonSystemItem: .play , target: self, action: #selector(playBtnAction(sender:)))
self.pauseBtn = UIBarButtonItem(barButtonSystemItem: .pause , target: self, action: #selector(pauseBtnAction(sender:)))
self.navigationItem.rightBarButtonItem = playBtn
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func playBtnAction(sender: UIBarButtonItem)
{
self.navigationItem.rightBarButtonItem = pauseBtn
self.labelMusicState.text = "Music is Playing"
}
func pauseBtnAction(sender: UIBarButtonItem)
{
self.navigationItem.rightBarButtonItem = playBtn
self.labelMusicState.text = "Music Not Playing"
}
}
Upvotes: 2
Reputation: 152
In both the scenarios, you are trying to assign .play button to your existing button whenever the player starts playing music. It should be opposite. whenever the player starts playing music, your button should change to .pause mode and when the player stops playing music, the button should go to .play mode.
do the following modification:
let playBtn = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: nil)
let pauseBtn = UIBarButtonItem(barButtonSystemItem: .pause, target: self, action: nil)
@IBAction func togglePausePlay(sender: UIBarButtonItem) {
if !audioPlayer.isPlaying {
audioPlayer.play()
pausePlayBtn = pauseBtn
} else {
audioPlayer.stop()
pausePlayBtn = playBtn
}
}
You were doing the opposite due to which once the music starts playing, the button does not change its state from play to pause but remains in play state due to which the next time user presses the button, it plays the music and doesnt stop it.
Upvotes: 0