Search code examples
javascriptaudiotabsmedia

Open media in new page instead of download (MediaRecorder-sample)


I am using this script to be able to record audio from the browser MediaRecorder-sample

This script creates an audio player and also a download link but I only want to have a link to open the player in a new window instead of downloading it. I'm not sure how to edit the download link.

This is the code as I have it now:

(HTML)

<div id='btns'>
    <div class="player">
      <button  class="btn btn-default" id='start'>start</button>
      <button  class="btn btn-default" id='stop'>stop</button>
      <button  class="btn btn-default" id='play'>play</button>
    </div>
</div>
<div>
  <ul  class="list-unstyled" id='ul'></ul>
</div>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

(CSS)

#start {
    height: 81px;
    width: 81px;
    background: url(images/rec-but.png) #C39 no-repeat;
    border: none;
}

#stop {
    height: 81px;
    width: 81px;
    background: url(images/pause-but.png) #C63 no-repeat;
    border: none;
}

#play {
    height: 81px;
    width: 81px;
    background: url(images/play-but.png) #900 no-repeat;
    border: none;
}

#play img {
    position: relative;
    left: -6px;
    top: -1px;
}

.player span {
        height: 81px;
    width: 81px;
    background: url(images/play-but.png) #699 no-repeat;
    border: solid 2px #F00;
    border-radius: 10px;
    display: inline-block;
    position: relative;
    /*top: -65px;*/
    top: -32px;
    left: -1px;
}

.player span a {
    display: inline-block;
    width: 81px;
    height: 81px;
    text-indent: -9999px;
}

.player {
    width: 267px;
    height: 98px;
    margin: 0 auto;
    background: url(images/playr-bg.png) #003 no-repeat;
    text-align: center;
    padding-top: 8px;
}

(JS)

'use strict'

let log = console.log.bind(console),
  id = val => document.getElementById(val),
  ul = id('ul'),
  start = id('start'),
  stop = id('stop'),
  stream,
  recorder,
  counter=1,
  chunks,
  media;


onload = e => {
  let mv = id('mediaVideo'),
      mediaOptions = {
        audio: {
          tag: 'audio',
          type: 'audio/ogg',
          ext: '.ogg',
          gUM: {audio: true}
        }
      };
  media =  mediaOptions.audio;
  navigator.mediaDevices.getUserMedia(media.gUM).then(_stream => {
    stream = _stream;
    id('btns').style.display = 'inherit';
    start.removeAttribute('disabled');
    recorder = new MediaRecorder(stream);
    recorder.ondataavailable = e => {
      chunks.push(e.data);
      if(recorder.state == 'inactive')  makeLink();
    };
    log('got media successfully');
  }).catch(log);
}

start.onclick = e => {
  start.disabled = true;
  document.getElementById('start').style.backgroundImage = "url(images/rec-but-active.png)";
  stop.removeAttribute('disabled');
  chunks=[];
  recorder.start();
}


stop.onclick = e => {
  stop.disabled = true;
  recorder.stop();
  document.getElementById('start').style.backgroundImage = "url(images/rec-but.png)";
  start.removeAttribute('disabled');
}



function makeLink(){
  let blob = new Blob(chunks, {type: media.type })
    , url = URL.createObjectURL(blob)
    , li = document.createElement('li')
    , hf = document.createElement('a')
  ;
  hf.href = url;
  hf.download = `${counter++}${media.ext}`;
  hf.innerHTML = `${hf.download}`;

  var emptyPlay = document.querySelector('#play');

    var newPlay = document.createElement('span');
    newPlay.appendChild(hf);
    hf.innerHTML = `${hf.download}`;

    emptyPlay.replaceWith(newPlay);

}

Any help would be appreciated


Solution

  • Your script is setting the download attribute of the a tag which indicate to the browser that this is a download link:

    hf.download = `${counter++}${media.ext}`;
    

    Just remove this attribute to make it work as a normal link.

    If you want the link to open in a new window/tab (depends on the browser configuration), add a target attribute to the a tag:

    hf.target = '_blank';