douyun1972 2013-03-18 22:10
浏览 58
已采纳

每次while循环运行时调用javascript以提供唯一的波形

I'm using the wavesurfer.js to generate a waveforms for audio clips, the problem I am having is that I have multiple audio files being pulled in a php while loop, and the javascript only seems to run for the first record returned..

I'll post what i've tried below, as you can see I have tried to make it unique and run every loop but I have run out of ideas and searched everywhere on the internet but nothing helped.

PHP:

 global $db;
 $select = $db->query("SELECT * FROM audio ORDER BY id DESC");
  while ($row = $select->fetch_assoc()) {
   echo $row["track_title"] . " by " . $row["artist"] . "<br />";
   $path = $row['path'];
   $path = str_replace("..", "", $path);
   $id = $row['id'];

    echo"
     <div class='waveid' id='".$id."'>
     <div class='path' id='".$path."'>
     <div class='cursor' id='wave-cursor'></div><canvas id='wave' class='wave-".$id."' width='1024' height='128'></canvas>
     </div>
     </div> 
    ";
    }

JAVASCRIPT:

var id = $(".waveid").attr('id');
var path = $(".path").attr('id');

console.log(id);
console.log(path);

var wavesurfer = Object.create(WaveSurfer);

wavesurfer.init({
canvas: document.querySelector('.wave-'+id),
waveColor: 'violet',
progressColor: 'purple'
});

wavesurfer.load(path);

To sum up, I have a while loop returning all the song data with the mp3's stored in a folder, the file paths are stored in the database and are being returned. I'm trying to make a unique waveform for each file but for some reason I can only manage to get the one waveform for the first record. Any help would be greatly appreciated.

EDIT: also if anyone wished to edit the title or any content/tags please feel free to do so.

  • 写回答

1条回答 默认 最新

  • dqq46733 2013-03-18 22:52
    关注

    PHP (you don't need to use divs):

    while ($row = $select->fetch_assoc()) {
     echo $row["track_title"] . " by " . $row["artist"] . "<br />";
     $path = $row['path'];
     $path = str_replace("..", "", $path);
     $id = $row['id'];
    
     # make sure $path doesn't contain ' character
     echo "<canvas class='wave' data-path='$path' data-id='$id' width='1024' height='128'></canvas>";
    }
    

    JS (this will make all waves):

    $('.wave').each(function(){
     var wavesurfer = Object.create(WaveSurfer);
    
     wavesurfer.init({
      canvas: this,
      waveColor: 'violet',
      progressColor: 'purple'
     });
    
     wavesurfer.load($(this).data('path'));
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论
  • ¥15 keil5 target not created
  • ¥15 C/C++数据与算法请教
  • ¥15 怎么找志同道合的伙伴
  • ¥20 如何让程序ab.eXe自已删除干净硬盘里的本文件自己的ab.eXe文件
  • ¥50 爬虫预算充足,跪巨佬
  • ¥15 滑块验证码拖动问题悬赏
  • ¥15 Wanted but not invoked:Actually, there were zero interactions with this moc
  • ¥20 怎么驱动tb6612