All files / ia-components/components/audio-players archive-audio-jwplayer-wrapper.jsx

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186                        1x   1x     1x           1x 1x 1x                 1x 1x 1x                                                                                                                                                                                                                         1x   1x                   2x   2x                   2x                                
import React, { Component } from 'react';
import PropTypes from 'prop-types';
 
/**
 * IA Audio Player
 * Uses global: Play class (IA JWPlayer wrapper), jwplayer
 *
 * It will display photo if given, and will overlay the media player at the base of the photo.
 *
 */
class ArchiveAudioPlayer extends Component {
  constructor(props) {
    super(props);
 
    this.jwPlayerInstance = React.createRef();
 
    // expecting jwplayer to be globally ready
    this.state = {
      player: null,
      jwplayerInstance: null,
      playerPlaylistIndex: null,
    };
 
    this.onPlaylistItemCB = this.onPlaylistItemCB.bind(this);
    this.playTrack = this.playTrack.bind(this);
    this.setURL = this.setURL.bind(this);
  }
 
  /**
   * Register jwplayer/play8 instance as component mounts on client
   */
  componentDidMount() {
    const {
      jwplayerInfo, jwplayerID, backgroundPhoto, onRegistrationComplete
    } = this.props;
    const { jwplayerPlaylist, identifier } = jwplayerInfo;
    Eif (!jwplayerPlaylist.length) return;
 
    const waveformer = backgroundPhoto
      ? {}
      : { waveformer: 'jw-holder' };
    // We are using IA custom global Player class to instatiate the player
    const baseConfig = {
      so: true,
      audio: true,
      identifier,
      hide_list: true,
      responsive: true,
      onPlaylistItem: this.onPlaylistItemCB,
      onReady: (jwplayerInstance) => {
        /**
         * Capture if player starts on track N+1
         */
        const playerPlaylistIndex = jwplayerInstance.getPlaylistIndex();
        this.setState({ playerPlaylistIndex, jwplayerInstance }, () => {
          this.props.jwplayerStartingPoint(playerPlaylistIndex);
        });
      }
    };
 
    if (window.Play && Play) {
      const compiledConfig = Object.assign({}, baseConfig, waveformer);
      const player = Play(jwplayerID, jwplayerPlaylist, compiledConfig);
      this.setState({ player });
 
      if (onRegistrationComplete) {
        /**
         * Currently, this is where we support external ability to set URL
         * through Internet Archive's JWPlayer Wrapper
         */
        onRegistrationComplete(this.setURL);
      }
    }
  }
 
  /**
   * Check if track index has changed. If so, then play that track
   */
  componentDidUpdate(prevProps) {
    const { sourceData: { index = null } } = this.props;
    const { sourceData: prevSourceData } = prevProps;
    const { index: prevIndex } = prevSourceData;
    const { jwplayerInstance, playerPlaylistIndex } = this.state;
    const indexIsNumber = Number.isInteger(index);
 
    if (!indexIsNumber) return;
 
    const isNowMainPlayer = !prevSourceData.hasOwnProperty('index') && indexIsNumber;
 
    if (isNowMainPlayer) {
      // do not autoplay this track any longer
      return;
    }
    const incomingTrackChange = index !== prevIndex;
 
    const isOnSameTrack = playerPlaylistIndex === index;
    const playerStatus = jwplayerInstance.getState();
    const playCurrentTrack = isOnSameTrack && (playerStatus === 'idle');
    const playTrack = indexIsNumber && (playCurrentTrack || incomingTrackChange);
    if (playTrack) {
      this.playTrack({ playerPlaylistIndex: index - 1 || 0 });
    }
  }
 
  /**
   * Event Handler that fires when JWPlayer starts a new track (eg: controlbar or auto-advance)
   */
  onPlaylistItemCB(jwplayer, event) {
    const { index } = event;
    const { playerPlaylistIndex } = this.state;
 
    if (playerPlaylistIndex !== index) {
      const { jwplayerPlaylistChange } = this.props;
      jwplayerPlaylistChange({ newTrackIndex: index });
    }
  }
 
  /**
   * Signals to IA's jwplayer handler, Play8,
   * that it's time to change the URL to match given track
   *
   * @param { number } trackNumber
   */
  setURL(trackNumber) {
    const { player } = this.state;
    const playlistIndex = trackNumber - 1 || 0;
    return player.playN(playlistIndex, true);
  }
 
  /**
   * This updates internal state & then tells jwplayer/Play8 to start playing track
   *
   * @param { Object } stateToUpdate
   * @param { number } stateToUpdate.playerPlaylistIndex - Track index to play. *Required
   * @param { * } stateToUpdate[param] - optional states to update
   */
  playTrack(stateToUpdate) {
    const { playerPlaylistIndex } = stateToUpdate;
    const { player } = this.state;
    this.setState(stateToUpdate, () => {
      player.playN(playerPlaylistIndex);
    });
  }
 
  render() {
    const { jwplayerID, style } = this.props;
 
    return (
      <div className="ia-player-wrapper" style={style}>
        <div className="iaux-player-wrapper">
          <div id={jwplayerID} />
        </div>
      </div>
    );
  }
}
 
ArchiveAudioPlayer.displayName = 'ArchiveAudioPlayer';
 
ArchiveAudioPlayer.defaultProps = {
  backgroundPhoto: '',
  jwplayerID: '',
  jwplayerPlaylistChange: null,
  jwplayerInfo: {},
  sourceData: null,
  onRegistrationComplete: null,
  style: ''
};
 
ArchiveAudioPlayer.propTypes = {
  backgroundPhoto: PropTypes.string,
  jwplayerID: PropTypes.string,
  jwplayerPlaylistChange: PropTypes.func,
  jwplayerInfo: PropTypes.shape({
    jwplayerPlaylist: PropTypes.array,
    identifier: PropTypes.string
  }),
  sourceData: PropTypes.shape({
    index: PropTypes.number
  }),
  onRegistrationComplete: PropTypes.func,
  style: PropTypes.object
};
 
export default ArchiveAudioPlayer;