How can I play .spx file by html5 ?

2019-04-28 08:55发布

from html5 spec, it seem support spx: http://dev.w3.org/html5/spec-preview/the-source-element.html

Using:

But from my trying, it can't play in both Firefox 17 and Chrome, could you help ?

2条回答
欢心
2楼-- · 2019-04-28 09:10

I have found that speex.js on GitHub (https://github.com/jpemartins/speex.js) can solve your problem. With speex.js you can demux & decode speex file (*.spx or *.ogg) to wav on fly, which is supported by both Chrome/Firefox and many other modern browsers HTML5 ready.

<script src="bitstring.js"></script>
<script src="pcmdata.min.js"></script>
<script src="speex.js"></script>
  • function below will do the trick to convert spx to wav codec:
/**
  * @param bufSpx ArrayBuffer (Uint8Array) holding content of speex file (*.spx or *.ogg)
  */
function decodeFile(bufSpx) {
  var stream, samples, st;
  var ogg, header, err;

  ogg = new Ogg(bufSpx, {file: true});
  ogg.demux();
  stream = ogg.bitstream();

  header = Speex.parseHeader(ogg.frames[0]);
  console.log(header);

  comment = new SpeexComment(ogg.frames[1]);
  console.log(comment.data);

  st = new Speex({
    quality: 8,
    mode: header.mode,
    rate: header.rate
  });

  samples = st.decode(stream, ogg.segments);

  var waveData = PCMData.encode({
      sampleRate: header.rate,
      channelCount: header.nb_channels,
      bytesPerSample: 2,
      data: samples
    });

  // array buffer holding audio data in wav codec
  var bufWav = Speex.util.str2ab(waveData);
  // convert to a blob object
  var blob = new Blob([bufWav], {type: "audio/wav"});
  // return a "blob://" url which can be used as a href anywhere
  return URL.createObjectURL(blob);
}
查看更多
在下西门庆
3楼-- · 2019-04-28 09:28

The spec says:

The type attribute gives the type of the media resource, to help the user agent determine if it can play this media resource before fetching it.

The spec itself does not specify any audio or video formats to be supported and support is up to individual browsers.

... and no browser supports .spx as far as I know.

查看更多
登录 后发表回答