douliang4858 2016-05-19 21:49
浏览 395

加载流音频文件html5音频播放器

I've written a service in golang with two endpoints - one requires the user to authorize in order to access it - the other requires no authorization.

I'm retrieving a stream from a music streaming API on the server and writing it back to the client as a response in both cases.

The content type on the response in both cases seems to be application/octet-stream. I've tried setting the content type to audio/mpeg but it seems to make no difference.

When I try to call load on the html5 audio player I'm using (just the straight up element) after setting the source to the return of my web service call to the response from the service, I get an error - undefined:1 Uncaught (in promise) DOMException: Failed to load because no supported source was found.

Here's how I'm retrieving the stream :

callApi: function() {
$.ajax({
  context: this,
  url: 'http://localhost:3001/secured/ping',
  method: 'GET'
}).then(function(data, textStatus, jqXHR) {
  alert("The request to the secured endpoint was successful");
  this.setState({audioSrc: data});
  this.refs.audio.play();
}, function() {
  alert("You need to download the server seed and start it to call this API");
});

}

The HTML5 audio element looks like -

<div className="logged-in-box auth0-box logged-in">
          <h1 id="logo"><img src="https://cdn.auth0.com/blog/auth0_logo_final_blue_RGB.png" /></h1>
          <audio ref="audio" src={this.state.audioSrc} autoPlay controls></audio>
          <img src={this.state.profile.picture} />
          <h2>Welcome {this.state.profile.nickname}</h2>
          <button onClick={this.callApi} className="btn btn-lg btn-primary">Call API</button>
        </div>

I'm using react as the client side framework for data binding etc... I'm not sure what that error is all about however I have a feeling it has to do with my response coming back as binary which the audio player can't handle.

Any ideas on how to fix this?

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么