SwiftAudio
SwiftAudio copied to clipboard
Regarding stop button functionality
i have a button of play and stop the play pause functionality working fine but when i click on stop button it resets everything which works but when i play again nothing happens..please help
I have faced same problem in my current project but i have handle like this.
player?.event.playbackEnd.addListener(self, handleAudioPlayerStatePlayBackEnd)
func handleAudioPlayerStatePlayBackEnd(state: AudioPlayer.PlaybackEndEventData) {
if state == .playedUntilEnd{
// Reload your audio again and reset UI also
}
}
}
not working @sukh1993 sir,
my code is as follows, can you point me where i am wrong where to add what code
var audioURL:String? private var isScrubbing: Bool = false private let audioPlayer = AudioPlayer() let audioSessionController = AudioSessionController.shared override func viewDidLoad() { super.viewDidLoad() setupAudioPlayer() configureTrackSlider() createNowPlayingAnimation() }
@IBAction func onClick_playAudioBtnTapped(_ sender: UIButton) { if !audioSessionController.audioSessionIsActive { try? audioSessionController.activateSession() } audioPlayer.togglePlaying() }
@IBAction func onClick_stopBtnTapped(_ sender: UIButton) { self.audioPlayer.stop() self.nowPlayingImageView.stopAnimating() }
private func setupAudioPlayer() { audioPlayer.event.stateChange.addListener(self, handleAudioPlayerStateChange) audioPlayer.event.secondElapse.addListener(self, handleAudioPlayerSecondElapsed) audioPlayer.event.seek.addListener(self, handleAudioPlayerDidSeek) audioPlayer.event.updateDuration.addListener(self, handleAudioPlayerUpdateDuration) audioPlayer.event.didRecreateAVPlayer.addListener(self, handleAVPlayerRecreated) audioPlayer.event.playbackEnd.addListener(self, handlePlayEnd) audioPlayer.event.fail.addListener(self, handlePlayerFailure) handleAudioPlayerStateChange(data: audioPlayer.playerState)
if let url = self.audioURL {
let item = DefaultAudioItem(audioUrl: url, sourceType: .stream)
try? self.audioPlayer.load(item: item, playWhenReady: true)
self.playPauseBtn.setImage(UIImage(named: "ic_audio_pause"), for: .normal)
self.scaleInBookCover(isFirstLoad: false)
self.nowPlayingImageView.startAnimating()
}
}
// MARK: - AudioPlayer Event Handlers extension AudioPlayerVC {
func updateTimeValues() {
self.trackSlider.maximumValue = CGFloat(self.audioPlayer.duration)
self.trackSlider.setValue(CGFloat(self.audioPlayer.currentTime), animated: true)
self.currentTimeLbl.text = getTimeString(time: self.audioPlayer.currentTime)
self.durationLbl.text = getTimeString(time: (self.audioPlayer.duration - self.audioPlayer.currentTime))
}
func setPlayButtonState(forAudioPlayerState state: AudioPlayerState) {
if state == .playing {
playPauseBtn.setImage(UIImage(named: "ic_audio_pause"), for: .normal)
self.scaleInBookCover(isFirstLoad: false)
self.nowPlayingImageView.startAnimating()
} else {
playPauseBtn.setImage(UIImage(named: "ic_audio_play"), for: .normal)
self.scaleOutBookCover()
self.nowPlayingImageView.stopAnimating()
}
}
func handleAudioPlayerStateChange(data: AudioPlayer.StateChangeEventData) {
print(data)
DispatchQueue.main.async {
self.setPlayButtonState(forAudioPlayerState: data)
switch data {
case .loading:
self.startLoader()
self.updateTimeValues()
case .buffering:
self.startLoader()
case .ready:
self.stopLoader()
self.updateTimeValues()
case .playing, .paused, .idle:
self.stopLoader()
self.updateTimeValues()
}
}
}
func handlePlayEnd(data: AudioPlayer.PlaybackEndEventData) {
DispatchQueue.main.async {
switch data {
case .playedUntilEnd:
if let url = self.audioURL {
let item = DefaultAudioItem(audioUrl: url, sourceType: .stream)
try? self.audioPlayer.load(item: item, playWhenReady: false)
}
case .playerStopped:
self.audioPlayer.stop()
self.stopLoader()
case .skippedToNext:
self.stopLoader()
case .skippedToPrevious:
self.stopLoader()
case .jumpedToIndex:
self.stopLoader()
}
}
}
func handleAudioPlayerSecondElapsed(data: AudioPlayer.SecondElapseEventData) {
if !isScrubbing {
DispatchQueue.main.async {
self.updateTimeValues()
}
}
}
func handleAudioPlayerDidSeek(data: AudioPlayer.SeekEventData) {
isScrubbing = false
}
func handleAudioPlayerUpdateDuration(data: AudioPlayer.UpdateDurationEventData) {
DispatchQueue.main.async {
self.updateTimeValues()
}
}
func handleAVPlayerRecreated() {
try? audioSessionController.set(category: .playback)
}
func handlePlayerFailure(data: AudioPlayer.FailEventData) {
if let error = data as NSError? {
if error.code == -1009 {
DispatchQueue.main.async {
print("Network disconnected. Please try again...")
}
}
}
}
}
@nishant6042 try this its works for me
func handlePlayEnd(data: AudioPlayer.PlaybackEndEventData) {
DispatchQueue.main.async {
switch data {
case .playedUntilEnd:
// pause audio player when playedUntilEnd and reload next song or previous song
self.audioPlayer.pause()
if let url = self.audioURL {
let item = DefaultAudioItem(audioUrl: url, sourceType: .stream)
try? self.audioPlayer.load(item: item, playWhenReady: false)
}
case .playerStopped:
self.audioPlayer.stop()
self.stopLoader()
case .skippedToNext:
self.stopLoader()
case .skippedToPrevious:
self.stopLoader()
case .jumpedToIndex:
self.stopLoader()
}
}
}
not working @sukh1993 sir, i have done audiolayer.stop() in stop button click and in playedUntilEnd i have done as you said
And actually i my case theres no next song when current song ends it just have to start the same song again but when i click stop button the ui resets but when play again it doesnt do anything
pls help @sukh1993 sir
@nishant6042 hi //If things is not working try this with some tricks
var isAudioStops : Bool = false
@IBAction func onClick_playAudioBtnTapped(_ sender: UIButton) {
if isAudioStops == true{
if let url = self.audioURL {
let item = DefaultAudioItem(audioUrl: url, sourceType: .stream)
try? self.audioPlayer.load(item: item, playWhenReady: true)
self.playPauseBtn.setImage(UIImage(named: "ic_audio_pause"), for: .normal)
self.scaleInBookCover(isFirstLoad: false)
self.nowPlayingImageView.startAnimating()
}
isAudioStops = false
}else{
if !audioSessionController.audioSessionIsActive {
try? audioSessionController.activateSession()
}
audioPlayer.togglePlaying()
}
}
@IBAction func onClick_stopBtnTapped(_ sender: UIButton) {
self.audioPlayer.stop()
self.nowPlayingImageView.stopAnimating()
self.isAudioStops = true // bool true
}
func handlePlayEnd(data: AudioPlayer.PlaybackEndEventData) {
DispatchQueue.main.async {
switch data {
case .playedUntilEnd:
self.isAudioStops = true // bool true
case .playerStopped:
self.audioPlayer.stop()
self.stopLoader()
case .skippedToNext:
self.stopLoader()
case .skippedToPrevious:
self.stopLoader()
case .jumpedToIndex:
self.stopLoader()
}
}
}
not working still @sukh1993 sir
@nishant6042 check with break points, Is your player reload the url or not on play button. if It is not working change stop method to pause.
@sukh1993 sir working when doing pause instead of stop but doing that it pauses i want to restart from start when stop clicked
@nishant6042 I will working this issue, this is bug on swift Audio side.
So whats the alternate for now?
and also what should i do to play in background also when i leave the screen @sukh1993 sir
Hi @nishant6042 Are you manage cache? means i load one audio and then play again, it will play from without load. If you did this please let me know, how can be?
@nishant6042 also check this link :- https://github.com/jorgenhenrichsen/SwiftAudio/issues/53 about stop functionality