src/playlist-loader.js
/**
    * When called without any arguments, returns the currently
    * active media playlist. When called with a single argument,
    * triggers the playlist loader to asynchronously switch to the
    * specified media playlist. Calling this method while the
    * loader is in the HAVE_NOTHING causes an error to be emitted
    * but otherwise has no effect.
    *
    * @param {Object=} playlist the parsed media playlist
    * object to switch to
    * @param {Boolean=} is this the last available playlist
    *
    * @return {Playlist} the current loaded media
    */
When called without any arguments, returns the currently
active media playlist. When called with a single argument,
triggers the playlist loader to asynchronously switch to the
specified media playlist. Calling this method while the
loader is in the HAVE_NOTHING causes an error to be emitted
but otherwise has no effect.

@param {Object=} playlist the parsed media playlist
object to switch to
@param {Boolean=} is this the last available playlist

@return {Playlist} the current loaded media
When called without any arguments returns the currently active media playlist . When called with a single argument triggers the playlist loader to asynchronously switch to the specified media playlist . Calling this method while the loader is in the HAVE_NOTHING causes an error to be emitted but otherwise has no effect . @param { Object = } playlist the parsed media playlist object to switch to @param { Boolean = } is this the last available playlist @return { Playlist } the current loaded media
media(playlist, isFinalRendition) {
    if (!playlist) {
      return this.media_;
    }
    if (this.state === 'HAVE_NOTHING') {
      throw new Error('Cannot switch media playlist from ' + this.state);
    }
    if (typeof playlist === 'string') {
      if (!this.master.playlists[playlist]) {
        throw new Error('Unknown playlist URI: ' + playlist);
      }
      playlist = this.master.playlists[playlist];
    }
    window.clearTimeout(this.finalRenditionTimeout);
    if (isFinalRendition) {
      const delay = (playlist.targetDuration / 2) * 1000 || 5 * 1000;
      this.finalRenditionTimeout =
        window.setTimeout(this.media.bind(this, playlist, false), delay);
      return;
    }
    const startingState = this.state;
    const mediaChange = !this.media_ || playlist.uri !== this.media_.uri;
    if (this.master.playlists[playlist.uri].endList) {
      if (this.request) {
        this.request.onreadystatechange = null;
        this.request.abort();
        this.request = null;
      }
      this.state = 'HAVE_METADATA';
      this.media_ = playlist;
      if (mediaChange) {
        this.trigger('mediachanging');
        this.trigger('mediachange');
      }
      return;
    }
    if (!mediaChange) {
      return;
    }
    this.state = 'SWITCHING_MEDIA';
    if (this.request) {
      if (playlist.resolvedUri === this.request.url) {
        return;
      }
      this.request.onreadystatechange = null;
      this.request.abort();
      this.request = null;
    }
    if (this.media_) {
      this.trigger('mediachanging');
    }
    this.request = this.hls_.xhr({
      uri: playlist.resolvedUri,
      withCredentials: this.withCredentials
    }, (error, req) => {
      if (!this.request) {
        return;
      }
      playlist.resolvedUri = resolveManifestRedirect(this.handleManifestRedirects, playlist.resolvedUri, req);
      if (error) {
        return this.playlistRequestError(this.request, playlist.uri, startingState);
      }
      this.haveMetadata(req, playlist.uri);
      if (startingState === 'HAVE_MASTER') {
        this.trigger('loadedmetadata');
      } else {
        this.trigger('mediachange');
      }
    });
  }
media ( playlist , isFinalRendition ) { if ( ! playlist ) { return this . media_ ; } if ( this . state === 'HAVE_NOTHING' ) { throw new Error ( 'Cannot switch media playlist from ' + this . state ) ; } if ( typeof playlist === 'string' ) { if ( ! this . master . playlists [ playlist ] ) { throw new Error ( 'Unknown playlist URI: ' + playlist ) ; } playlist = this . master . playlists [ playlist ] ; } window . clearTimeout ( this . finalRenditionTimeout ) ; if ( isFinalRendition ) { const delay = ( playlist . targetDuration / 2 ) * 1000 || 5 * 1000 ; this . finalRenditionTimeout = window . setTimeout ( this . media . bind ( this , playlist , false ) , delay ) ; return ; } const startingState = this . state ; const mediaChange = ! this . media_ || playlist . uri !== this . media_ . uri ; if ( this . master . playlists [ playlist . uri ] . endList ) { if ( this . request ) { this . request . onreadystatechange = null ; this . request . abort ( ) ; this . request = null ; } this . state = 'HAVE_METADATA' ; this . media_ = playlist ; if ( mediaChange ) { this . trigger ( 'mediachanging' ) ; this . trigger ( 'mediachange' ) ; } return ; } if ( ! mediaChange ) { return ; } this . state = 'SWITCHING_MEDIA' ; if ( this . request ) { if ( playlist . resolvedUri === this . request . url ) { return ; } this . request . onreadystatechange = null ; this . request . abort ( ) ; this . request = null ; } if ( this . media_ ) { this . trigger ( 'mediachanging' ) ; } this . request = this . hls_ . xhr ( { uri : playlist . resolvedUri , withCredentials : this . withCredentials } , ( error , req ) => { if ( ! this . request ) { return ; } playlist . resolvedUri = resolveManifestRedirect ( this . handleManifestRedirects , playlist . resolvedUri , req ) ; if ( error ) { return this . playlistRequestError ( this . request , playlist . uri , startingState ) ; } this . haveMetadata ( req , playlist . uri ) ; if ( startingState === 'HAVE_MASTER' ) { this . trigger ( 'loadedmetadata' ) ; } else { this . trigger ( 'mediachange' ) ; } } ) ; }
When called without any arguments, returns the currently
 active media playlist.
When called without any arguments returns the currently active media playlist .
// getter // setter // find the playlist object if the target playlist has been // specified by URI // switch to fully loaded playlists immediately // abort outstanding playlist requests // trigger media change if the active media has been updated // switching to the active playlist is a no-op // there is already an outstanding playlist request // requesting to switch to the same playlist multiple times // has no effect after the first // request the new playlist // disposed // fire loadedmetadata the first time a media playlist is loaded