fethica / FRadioPlayer

A simple radio player framework for iOS, macOS, tvOS.

Home Page:https://fethica.github.io/FRadioPlayer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot stop radio while it's loading

fahlout opened this issue · comments

I seem to not be able to stop the radio from playing while it's still loading. Is there anyway to cancel loading and keep it from starting to play while it's in the loading state?

Hi @fahlout , when the player is stopped in the loading state, the playbackStateDidChange gets called with FRadioPlaybackState.stopped, and yes the player state stays in FRadioPlayerState.loading, I should add another state to handle this case, in the meantime you could reset your UI using playbackStateDidChange method when the player is stopped!

Thanks for reporting the issue.

I found the problem! Thanks, I will try to implement a proper solution to this issue.

Perfect! Looking forward to the update.

Hey Fethi! Nice player! Great work! Have you solved this issue?

I'm having an issue when the player.radioURL is changed fast, the previous is played.
Let's say I have 3 streams: a, b and c.
When I change from a->b->c, b is played. I'm trying to call stop before i change the radioUrl, but with no success.

Any thoughts?

Hey @raskri ,

Yes it's the same bug, there is an async call for setting the playerItem causing this issue, I will try to fix it in the next release!

So changing the radioURLDidChange function to this solved my problem:

private func radioURLDidChange(with url: URL?) {
  resetPlayer()
  guard let url = url else { state = .urlNotSet; return }
  state = .loading
  player = AVPlayer()
  self.theUrlToPlay = url.absoluteString
                
  preparePlayer(with: AVAsset(url: url)) { (success, asset) in
    guard success, let asset = asset else {
      self.resetPlayer()
      self.state = .error
      return
    }
    
    // still on same url?
    if(self.theUrlToPlay == url.absoluteString){
      self.playerItem = AVPlayerItem(asset: asset)
    }
  }
}

It works
Captura de Pantalla 2020-03-22 a la(s) 23 11 45

Provided solutions did not work for me. However,
Upon further investigation, the slow loading stations set the station playback state to .loading and when you quickly switch to another station, when the .loadingFinished state is done, even though the player.radioURL object is printed as the latest selected station, it was playing the station that was lastly in the .loading state

So I created a variable loadingForStationURL
And set its value to the loading radio stations URL. If this variable does not match the .loadingFinished's url, I reset the radioUrl and played again.

func radioPlayer(_ player: FRadioPlayer, playerStateDidChange state: FRadioPlayerState) {
        if state == .loading {
            loadingForStationURL = player.radioURL
        }
        
        if state == .loadingFinished {
            if player.radioURL != loadingForStationURL {
                player.stop()
                playStation(url: player.radioURL!)
            }
        }
    }

Hope this solution works for you all :)