Giter VIP home page Giter VIP logo

Comments (15)

nishant6042 avatar nishant6042 commented on June 19, 2024 1

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

from swiftaudio.

nishant6042 avatar nishant6042 commented on June 19, 2024 1

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

from swiftaudio.

nishant6042 avatar nishant6042 commented on June 19, 2024 1

pls help @sukh1993 sir

from swiftaudio.

sukh1993 avatar sukh1993 commented on June 19, 2024

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

from swiftaudio.

nishant6042 avatar nishant6042 commented on June 19, 2024

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

}

from swiftaudio.

sukh1993 avatar sukh1993 commented on June 19, 2024

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

from swiftaudio.

sukh1993 avatar sukh1993 commented on June 19, 2024

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

from swiftaudio.

nishant6042 avatar nishant6042 commented on June 19, 2024

not working still @sukh1993 sir

from swiftaudio.

sukh1993 avatar sukh1993 commented on June 19, 2024

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

from swiftaudio.

nishant6042 avatar nishant6042 commented on June 19, 2024

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

from swiftaudio.

sukh1993 avatar sukh1993 commented on June 19, 2024

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

from swiftaudio.

nishant6042 avatar nishant6042 commented on June 19, 2024

So whats the alternate for now?

from swiftaudio.

nishant6042 avatar nishant6042 commented on June 19, 2024

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

from swiftaudio.

sukh1993 avatar sukh1993 commented on June 19, 2024

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?

from swiftaudio.

sukh1993 avatar sukh1993 commented on June 19, 2024

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

from swiftaudio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.