weixin_33714884 2017-12-28 08:00 采纳率: 0%
浏览 314

使用Ajax在HTML中加载音频

I have a list of music I want When I click image of music opened media player with ajax, I Use this code in HTML:

 <div class="container ">
     <div class="row center">
        @foreach($singles as $single)
           <div class="col-md-2 folio-thumb">
             <a href="#" data-value="http://media.w3.org/2010/07/bunny/04-
               Death_Becomes_Fur.oga"><img class="img-responsive"
                src={{asset($single->Single_Image)}} /></a>
                     <h6 style="color:white" id="music">
                      {{$single>Single_Name}}<a href="/"></a></h6>
           </div>
        @endforeach
      </div>
   </div>
   <audio id="audio" controls="controls" style="margin-left:150px;">
      <source id="audioSource" src="">
       Your browser does not support the audio format.
   </audio>

And Ajax Code:

$('.folio-thumb').click(function (e) {
    e.preventDefault();
     var elm = e.target;
      var audio = document.getElementById('audio');
        var source = document.getElementById('audioSource');
         source.src = elm.getAttribute('data-value');
          audio.load(); 
         audio.play(); 
   });

But I Give this Error in console:

GET http://127.0.0.1:8000/null net::ERR_ABORTED home:288 Uncaught (in promise) DOMException: The play() request was interrupted by a new load request.

  • 写回答

2条回答 默认 最新

  • weixin_33716557 2017-12-28 08:22
    关注

    You are using selector .folio-thumb even that class in for the wrapping <div><div class="col-md-2 folio-thumb">. Which results var source = document.getElementById('audioSource'); returning null. (The div does not have any data attributes).

    You should add selector to the <a> element and use that in the JavaScript.

    Example; <a class="play-button" href="#" data-value="http://media.w3.org/2010/07/bunny/04-Death_Becomes_Fur.oga">...</a>

    $('.play-button').click(function (e) {
        e.preventDefault();
         var elm = e.target;
          var audio = document.getElementById('audio');
            var source = document.getElementById('audioSource');
             source.src = elm.getAttribute('data-value');
              audio.load(); 
             audio.play(); 
       });
    

    Working example at https://jsbin.com/ceqotimuru/edit?html,js,output

    Best regards, Riku

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀