Html 实现amr文件播放

主要实现方式:

1.将amr文件转化成base64字符串

2.用js转换成音频播放

js文件下载链接: https://pan.baidu.com/s/1kWsLOc3 密码: 2ca5

链接: https://pan.baidu.com/s/1smk2fdN 密码: qcbw

https://blog.csdn.net/hanglove_lucky/article/details/79106314

服务端:将网络文件转为 base64格式

public void fileToBase64(){
   String urlStr = getPara("url");

   URL url = null;
   try {
      url = new URL(urlStr);
      HttpURLConnection conn = (HttpURLConnection)url.openConnection();
      //设置超时间为3秒
      conn.setConnectTimeout(3*1000);
      //防止屏蔽程序抓取而返回403错误
      conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

      //得到输入流
      InputStream inputStream = conn.getInputStream();
      byte[] buffer = new byte[1024];
      int len = 0;
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      while((len = inputStream.read(buffer)) != -1) {
         bos.write(buffer, 0, len);
      }
      String text =  Base64.encode(bos.toByteArray());
      bos.close();
      System.out.println(text);
      renderText(text);
      return;
   } catch (MalformedURLException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   }

   renderText("");
}

前端页面:
 
<!DOCTYPE HTML>
<html>
<head>
<script src="audio.min.js"></script>
</head>
<script src="voice-2.0.js"></script>
 
<script>
 
 
  audiojs.events.ready(function() {
    var as = audiojs.createAll();
  });
</script>
<body>
 
<input type="button" value="play" id="playId"/>
<input type="button" value="stop" id="stopId"/>
 
</body>
<script type="text/javascript">
RongIMLib.RongIMVoice.init();
document.getElementById("playId").onclick = function(){
 $.ajax({ url: "fileToBase64", data: { url:url }, cache: false, dataType: "text", success:       function(_data) { if(_data != ""){ RongIMLib.RongIMVoice.play(_data); } }, error:function (xhr){ } });
};
document.getElementById("stopId").onclick = function(){
    RongIMLib.RongIMVoice.stop();
};
</script>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值