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 class="page-wrapper">
<h1 style="margin-bottom: -20px; font-weight: 300;">Vue.js & Wavesurfer.js Audio Player Component</h1>
<div id="wavesurfer-components">
      
<!-- PLAY WITH COMPONENT HERE -->
  
<mpakt-player 
track-url="https://mpakt.nyc3.digitaloceanspaces.com/parker-charles/demo-music/radar.mp3" 
track-name="Leotrix - Radar" />

      
      
</div>
<div class="pen-notes">
  <p><em>Click on the waveform to move forwards of backwards.</em></p>
  <p>This is an audio player component made using Vue.js and Wavesurfer.js. It is suitable for short audio files such as songs or short audio clips. The primary puropse of this pen was to experiment with a "<em>checkbox input</em> play button" styling and
    gradient styling (fading out waveform). I don't reccommend using this component for long audio files due to performance reasons. Audio controls can be extended using the various <a href="https://wavesurfer-js.org/docs/methods.html">methods provided by wavesurfer.js</a>.</p>
  <h3>Props:</h3>
  <ul style="list-style: none; padding: 0; margin: 0;">
    <li><strong>track-url</strong> : link to the audio file</li>
    <li><strong>track-name</strong> : name of the track</li>
  </ul>
  <br />
  <p>Song: <a href="https://soundcloud.com/leotrixofficial/radar">Radar by Leotrix</a></p>
    <a href="https://mpakt.io/">Contact Parker</a>
</div>
</div>
              
            
!

CSS

              
                *,
*:before,
*:after {
  box-sizing: border-box;
}

$color-one: #ccc;
$color-two: #333;
$size: 50px;

body {
  text-align: center;
  color: #7f8c8d;
  box-sizing: border-box;
  font-family: Roboto;
}

.page-wrapper {
  background-color: #eee;
  padding: 10px;
  min-height: 100vh;
}

//PLAYER STYLES
.mpakt-player-container {
  background: linear-gradient(0.25turn, $color-two 10%, $color-one);
  height: 80px;
  display: flex;
  // border: 1px solid rgba( 130, 100, 255, 1);
  border-radius: 6px;
  padding: 10px 10px 10px 16px;
  margin-top: 60px;
  box-shadow: 0px 0px 30px $color-two inset, 0px 0px 1px 1px rgba(1, 1, 1, 0.1);
  transition: all 1s ease;
  max-width: 800px;
  margin-left: auto;
  margin-right: auto;
}

.mpakt-player-waveform {
  flex: auto;
  transition: all 1s ease-in;
  z-index: 3;
  margin-right: -10px;
}

.gradient-overlay {
  position: relative;
  height: 76px;
  margin-bottom: -65px;
  margin-top: -10px;
  margin-right: -20px;
  width: 80%;
  background: linear-gradient(90deg, $color-two, rgba(0, 0, 0, 0));
  z-index: 3;
  pointer-events: none;
}

.play-button {
  display: block;
  position: relative;
  width: 25px;
  height: 1fr;
  margin-top: -10px;
  margin-bottom: -10px;
  margin-right: -40px;
  margin-left: 5px;
  cursor: pointer;
  z-index: 4;
  opacity: 0.3;
  transition: opacity 0.2s ease-in;
  &:hover {
    opacity: 1;
  }
  input[type="checkbox"] {
    display: none;
    &:checked {
      ~ span {
        &::before,
        &::after {
          border: ($size / 4) solid transparent;
          border-left: ($size / 2) solid white;
          border-right: 0;
        }
        &::after {
          transform: translateY(-50%) scaleY(0.5);
        }
      }
    }
  }
  span {
    display: block;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
    width: ($size / 2);
    height: ($size / 2);
    &::before,
    &::after {
      content: "";
      display: block;
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      height: 100%;
      border: 0 solid transparent;
      border-left: ($size / 5) solid white;
      transition: all 0.4s ease;
      right: 0;
    }
    &::before {
      left: 0;
    }
  }
}
.track-title {
  position: absolute;
  font-size: 1em;
  color: white;
  border: 2px solid white;
  margin-top: 16px;
  margin-left: 50px;
  z-index: 6;
  padding: 3px 10px 4px 10px;
  background: rgba(1, 1, 1, 0.2);
  box-shadow: 0px 0px 5px rgba(1, 1, 1, 0.4);
  max-width: 70%;
  opacity: 0.3;
  transition: all 1s ease;
}
.track-title:hover {
  opacity: 1;
  transform: scale(1.03);
}
audio {
  //HIDES THE MEDIA ELEMENT
  display: none;
}

.pen-notes {
  width: 90%;
  margin: 40px auto 0px auto;
  max-width: 500px;
}

              
            
!

JS

              
                var mpaktPlayerCounter = 0;
// to-do: check for this variable and set it from within the component itself.


// Player Component
Vue.component("mpakt-player", {
  template:`
<div>
    <div class="mpakt-player-container">
      <label class="play-button"><input type="checkbox" @click="playPause" checked><span></span></label>
      <div :id="waveformElementId" class="mpakt-player-waveform">
          <div class="gradient-overlay"></div>
      </div>
      <p  v-if="trackName" class="track-title">{{trackName}}</p>
    </div>
</div>
`,  
  props: ["trackUrl", "trackName"],
  computed: {
    waveformElementId: () => {
      return "mpakt-player-" + mpaktPlayerCounter;
    }
  },
  methods: {
    playPause() {
      this.surfer.playPause();
      }
  },
  created(){ // Increment to produce waveFormElementId
    mpaktPlayerCounter += 1;
  },
  mounted() {
      this.surfer = WaveSurfer.create({
          container: "#" + this.waveformElementId, //Render waveform within this component 
          height: 57,
          fillParent: false,
          scrollParent: true,
          minPxPerSec: 60,
          mediaControls: true,
          cursorColor: "#fff",
          cursorWidth: 0,
          backend: "MediaElement",
          barWidth: 1,
          barGap: 0,
          hideScrollbar: true,
          pixelRatio: 1,
          partialRenter: true,
          progressColor: "#fff",
          waveColor: "#333",
          autoCenter: true
          });
      this.surfer.load(this.trackUrl); 
  }
});



// Vue Instance
new Vue({
  el: "#wavesurfer-components"
});

              
            
!
999px

Console