FRadioPlayer icon indicating copy to clipboard operation
FRadioPlayer copied to clipboard

Cannot stop radio while it's loading

Open fahlout opened this issue 5 years ago • 9 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?

fahlout avatar Oct 17 '18 18:10 fahlout

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.

fethica avatar Oct 22 '18 00:10 fethica

The problem I am having, however, is that when calling stop on the radio while in the loading state it will still start playing once it is done loading instead of suspending the loading process of the radio stream. It seems to ignore the stop call while in loading state.

Thanks for looking into this for me!

On Oct 21, 2018, at 7:36 PM, Fethi El Hassasna [email protected] wrote:

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 mean time you could reset your UI using playbackStateDidChange method when the player is stopped!

Thanks for reporting the issue.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

fahlout avatar Oct 22 '18 16:10 fahlout

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

fethica avatar Oct 23 '18 18:10 fethica

Perfect! Looking forward to the update.

fahlout avatar Oct 23 '18 19:10 fahlout

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?

raskri avatar Mar 26 '19 19:03 raskri

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!

fethica avatar Mar 29 '19 15:03 fethica

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

raskri avatar Sep 13 '19 20:09 raskri

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

aleeherasimiuk avatar Mar 23 '20 02:03 aleeherasimiuk

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 :)

sharingisnice avatar Oct 31 '21 19:10 sharingisnice