Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app"></div>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  background-size: 6px 6px !important;
  background-image: linear-gradient(-45deg, rgba(0, 0, 0, 0) 46%, coral 49%, coral 51%, rgba(0, 0, 0, 0) 55%);
  background-color: white;
  padding-top: 60px;
}

.audio-player {
  width: 470px;
  padding: 35px 20px;
  margin: auto;
  background-color: white;
  border: 1px solid black;

  .player-controls {
    position: relative;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  #radioIcon {
    width: 30px;
    height: 30px;
    background: url('https://image.flaticon.com/icons/svg/149/149429.svg') no-repeat center;
  }

  #playAudio {
    -webkit-appearance: none;
    outline: none;
    cursor: pointer;
    border: none;
    width: 30px;
    height: 30px;
    background: url('https://image.flaticon.com/icons/svg/149/149125.svg') no-repeat center;
    background-size: contain;

    &.pause {
      background: url('https://image.flaticon.com/icons/svg/149/149127.svg') no-repeat center;
      background-size: contain;
    }
  }

  p {
    margin: 0 0 0 5px;
    line-height: 1;
    display: inline-flex;

    small {
      font-size: 10px;
    }
  }

  #seekObjContainer {
    position: relative;
    width: 300px;
    margin: 0 5px;
    height: 5px;

    #seekObj {
      position:relative;
      width: 100%;
      height: 100%;
      background-color: #e3e3e3;
      border: 1px solid black;

      #percentage {
        position: absolute;
        left: 0;
        top: 0;
        height: 100%;
        background-color: coral;
      }
    }
  }
}
              
            
!

JS

              
                class AudioPlayer extends React.Component {

  constructor(props) {
    super(props);
    this.media = React.createRef();
    this.percentage = React.createRef();
    this.seekObj = React.createRef();
    this.currentTime = React.createRef();
  }

  state = {
    isPlaying: false
  };

togglePlay = () => {
  let audioIsPlaying = this.media.current.currentTime > 0 && !this.media.current.paused && this.media.current.readyState > 2;

  if (!audioIsPlaying) {
    this.media.current.play();
  } else {
    this.media.current.pause();
  }

  this.setState({
    isPlaying: !this.state.isPlaying
  });

};

calculatePercentPlayed = () => {
  let percentPlayed = (this.media.current.currentTime / this.media.current.duration).toFixed(2) * 100;
  this.percentage.current.style.width = `${percentPlayed}%`;
}

calculateCurrentValue = (currentTime) => {
  const currentMinute = parseInt(currentTime / 60) % 60;
  const currentSecondsLong = currentTime % 60;
  const currentSeconds = currentSecondsLong.toFixed();
  const currentTimeFormatted = `${currentMinute < 10 ? `0${currentMinute}` : currentMinute}:${
  currentSeconds < 10 ? `0${currentSeconds}` : currentSeconds
  }`;

  return currentTimeFormatted;
}

initProgressBar = () => {
  const media = this.media.current;
  const currentTime = this.calculateCurrentValue(media.currentTime);

  function seek(e) {
    const percent = e.offsetX / this.offsetWidth;
    media.currentTime = percent * media.duration;
  }

  this.currentTime.current.innerHTML = currentTime;
  this.seekObj.current.addEventListener('click', seek);
  this.calculatePercentPlayed();
}

onEnded = () => {
  this.percentage.current.style.width = 0;
  this.currentTime.current.innerHTML = '00:00';

  this.setState({
    isPlaying: false
  });
}

render() {
  const { src } = this.props;
  const isPlaying = this.state.isPlaying;

  return (

    <div className="audio-player">

      <audio ref={this.media} onTimeUpdate={this.initProgressBar} onEnded={this.onEnded} id="audio">
        <source src={src} type="audio/mp3" />
      </audio>

      <div className="player-controls">

        <div id="radioIcon"></div>

        <button onClick={this.togglePlay} className={ isPlaying == false ? 'play' : 'pause'} id="playAudio"></button>

        <div id="seekObjContainer">
          <div ref={this.seekObj} id="seekObj">
            <div id="percentage" ref={this.percentage}></div>
          </div>
        </div>

        <p><small id="currentTime" ref={this.currentTime}>00:00</small></p>

      </div>
    </div>
  );
}
}

class App extends React.Component {

  render() {
    const audioFile = "https://thenewcode.com/assets/audio/24-ghosts-III.mp3";

    return (
      <AudioPlayer src={audioFile} />
    );
  }
}

ReactDOM.render(<App />, document.getElementById('app'));
              
            
!
999px

Console