useAudio() {
    let track;

    this.audioTracks_.forEach((t) => {
      if (!track && t.enabled) {
        track = t;
      }
    });

    // called too early or no track is enabled
    if (!track) {
      return;
    }

    // Pause any alternative audio
    if (this.audioPlaylistLoader_) {
      this.audioPlaylistLoader_.pause();
      this.audioPlaylistLoader_ = null;
      this.audioSegmentLoader_.pause();
    }

    // If the audio track for the active audio group has
    // a playlist loader than it is an alterative audio track
    // otherwise it is a part of the mainSegmenLoader
    let loader = track.getLoader(this.activeAudioGroup());

    if (!loader) {
      this.mainSegmentLoader_.clearBuffer();
      return;
    }

    // TODO: it may be better to create the playlist loader here
    // when we can change an audioPlaylistLoaders src
    this.audioPlaylistLoader_ = loader;

    if (this.audioPlaylistLoader_.started) {
      this.audioPlaylistLoader_.load();
      this.audioSegmentLoader_.load();
      this.audioSegmentLoader_.clearBuffer();
      return;
    }

    this.audioPlaylistLoader_.on('loadedmetadata', () => {
      /* eslint-disable no-shadow */
      let media = this.audioPlaylistLoader_.media();
      /* eslint-enable no-shadow */

      this.audioSegmentLoader_.playlist(media, this.requestOptions_);
      this.addMimeType_(this.audioSegmentLoader_, 'mp4a.40.2', media);

      // if the video is already playing, or if this isn't a live video and preload
      // permits, start downloading segments
      if (!this.tech_.paused() ||
          (media.endList && this.tech_.preload() !== 'none')) {
        this.audioSegmentLoader_.load();
      }

      if (!media.endList) {
        // trigger the playlist loader to start "expired time"-tracking
        this.audioPlaylistLoader_.trigger('firstplay');
      }
    });

    this.audioPlaylistLoader_.on('loadedplaylist', () => {
      let updatedPlaylist;

      if (this.audioPlaylistLoader_) {
        updatedPlaylist = this.audioPlaylistLoader_.media();
      }

      if (!updatedPlaylist) {
        // only one playlist to select
        this.audioPlaylistLoader_.media(
          this.audioPlaylistLoader_.playlists.master.playlists[0]);
        return;
      }

      this.audioSegmentLoader_.playlist(updatedPlaylist, this.requestOptions_);
    });

    this.audioPlaylistLoader_.on('error', () => {
      videojs.log.warn('Problem encountered loading the alternate audio track' +
                       '. Switching back to default.');
      this.audioSegmentLoader_.abort();
      this.audioPlaylistLoader_ = null;
      this.useAudio();
    });

    this.audioSegmentLoader_.clearBuffer();
    this.audioPlaylistLoader_.start();
  }
/**
   * Use any audio track that we have, and start to load it
   */
Use any audio track that we have, and start to load it
Use any audio track that we have and start to load it
useAudio() {
    let track;
    this.audioTracks_.forEach((t) => {
      if (!track && t.enabled) {
        track = t;
      }
    });
    if (!track) {
      return;
    }
    if (this.audioPlaylistLoader_) {
      this.audioPlaylistLoader_.pause();
      this.audioPlaylistLoader_ = null;
      this.audioSegmentLoader_.pause();
    }
    let loader = track.getLoader(this.activeAudioGroup());
    if (!loader) {
      this.mainSegmentLoader_.clearBuffer();
      return;
    }
    this.audioPlaylistLoader_ = loader;
    if (this.audioPlaylistLoader_.started) {
      this.audioPlaylistLoader_.load();
      this.audioSegmentLoader_.load();
      this.audioSegmentLoader_.clearBuffer();
      return;
    }
    this.audioPlaylistLoader_.on('loadedmetadata', () => {
      let media = this.audioPlaylistLoader_.media();
      this.audioSegmentLoader_.playlist(media, this.requestOptions_);
      this.addMimeType_(this.audioSegmentLoader_, 'mp4a.40.2', media);
      if (!this.tech_.paused() ||
          (media.endList && this.tech_.preload() !== 'none')) {
        this.audioSegmentLoader_.load();
      }
      if (!media.endList) {
        this.audioPlaylistLoader_.trigger('firstplay');
      }
    });
    this.audioPlaylistLoader_.on('loadedplaylist', () => {
      let updatedPlaylist;
      if (this.audioPlaylistLoader_) {
        updatedPlaylist = this.audioPlaylistLoader_.media();
      }
      if (!updatedPlaylist) {
        this.audioPlaylistLoader_.media(
          this.audioPlaylistLoader_.playlists.master.playlists[0]);
        return;
      }
      this.audioSegmentLoader_.playlist(updatedPlaylist, this.requestOptions_);
    });
    this.audioPlaylistLoader_.on('error', () => {
      videojs.log.warn('Problem encountered loading the alternate audio track' +
                       '. Switching back to default.');
      this.audioSegmentLoader_.abort();
      this.audioPlaylistLoader_ = null;
      this.useAudio();
    });
    this.audioSegmentLoader_.clearBuffer();
    this.audioPlaylistLoader_.start();
  }
useAudio ( ) { let track ; this . audioTracks_ . forEach ( ( t ) => { if ( ! track && t . enabled ) { track = t ; } } ) ; if ( ! track ) { return ; } if ( this . audioPlaylistLoader_ ) { this . audioPlaylistLoader_ . pause ( ) ; this . audioPlaylistLoader_ = null ; this . audioSegmentLoader_ . pause ( ) ; } let loader = track . getLoader ( this . activeAudioGroup ( ) ) ; if ( ! loader ) { this . mainSegmentLoader_ . clearBuffer ( ) ; return ; } this . audioPlaylistLoader_ = loader ; if ( this . audioPlaylistLoader_ . started ) { this . audioPlaylistLoader_ . load ( ) ; this . audioSegmentLoader_ . load ( ) ; this . audioSegmentLoader_ . clearBuffer ( ) ; return ; } this . audioPlaylistLoader_ . on ( 'loadedmetadata' , ( ) => { let media = this . audioPlaylistLoader_ . media ( ) ; this . audioSegmentLoader_ . playlist ( media , this . requestOptions_ ) ; this . addMimeType_ ( this . audioSegmentLoader_ , 'mp4a.40.2' , media ) ; if ( ! this . tech_ . paused ( ) || ( media . endList && this . tech_ . preload ( ) !== 'none' ) ) { this . audioSegmentLoader_ . load ( ) ; } if ( ! media . endList ) { this . audioPlaylistLoader_ . trigger ( 'firstplay' ) ; } } ) ; this . audioPlaylistLoader_ . on ( 'loadedplaylist' , ( ) => { let updatedPlaylist ; if ( this . audioPlaylistLoader_ ) { updatedPlaylist = this . audioPlaylistLoader_ . media ( ) ; } if ( ! updatedPlaylist ) { this . audioPlaylistLoader_ . media ( this . audioPlaylistLoader_ . playlists . master . playlists [ 0 ] ) ; return ; } this . audioSegmentLoader_ . playlist ( updatedPlaylist , this . requestOptions_ ) ; } ) ; this . audioPlaylistLoader_ . on ( 'error' , ( ) => { videojs . log . warn ( 'Problem encountered loading the alternate audio track' + '. Switching back to default.' ) ; this . audioSegmentLoader_ . abort ( ) ; this . audioPlaylistLoader_ = null ; this . useAudio ( ) ; } ) ; this . audioSegmentLoader_ . clearBuffer ( ) ; this . audioPlaylistLoader_ . start ( ) ; }
Use any audio track that we have, and start to load it
Use any audio track that we have and start to load it
// called too early or no track is enabled // Pause any alternative audio // If the audio track for the active audio group has // a playlist loader than it is an alterative audio track // otherwise it is a part of the mainSegmenLoader // TODO: it may be better to create the playlist loader here // when we can change an audioPlaylistLoaders src /* eslint-disable no-shadow */ /* eslint-enable no-shadow */ // if the video is already playing, or if this isn't a live video and preload // permits, start downloading segments // trigger the playlist loader to start "expired time"-tracking // only one playlist to select