Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regarding stop button functionality #128

Open
nishant6042 opened this issue Oct 22, 2020 · 15 comments
Open

Regarding stop button functionality #128

nishant6042 opened this issue Oct 22, 2020 · 15 comments

Comments

@nishant6042
Copy link

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

@sukh1993
Copy link

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            
   }
 }
}

@nishant6042
Copy link
Author

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...")
            }
        }
    }
}

}

@sukh1993
Copy link

@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()
        }
    }
}

@nishant6042
Copy link
Author

not working @sukh1993 sir, i have done audiolayer.stop() in stop button click and in playedUntilEnd i have done as you said

@nishant6042
Copy link
Author

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

@nishant6042
Copy link
Author

pls help @sukh1993 sir

@sukh1993
Copy link

@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()
        }
    }
}

@nishant6042
Copy link
Author

not working still @sukh1993 sir

@sukh1993
Copy link

@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.

@nishant6042
Copy link
Author

@sukh1993 sir working when doing pause instead of stop but doing that it pauses i want to restart from start when stop clicked

@sukh1993
Copy link

@nishant6042 I will working this issue, this is bug on swift Audio side.

@nishant6042
Copy link
Author

So whats the alternate for now?

@nishant6042
Copy link
Author

and also what should i do to play in background also when i leave the screen @sukh1993 sir

@sukh1993
Copy link

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?

@sukh1993
Copy link

@nishant6042 also check this link :- #53
about stop functionality

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants