Search code examples
javascriptlaravelaudiodownloadplayback

How to play and download audio from database in laravel


please help me to fix the problem... This is my code to play and download audio from database. i am a newbie in coding.. so i don't have any knowledge to do the javascript part.. the audios are in database.. so i dont know how to get id

<table  id="mytable" class="table table-bordred table-striped">

     <thead>
    <th id="wid2"> Recording Id </th>
    <th id="wid1"> Unique Id</th>
    <th id="wid1"> Phone Number</th>
    <th id="wid1"> Date Time</th>
    <th id="wid1">Call Type</th>
    <th id="wid1"> Extension</th>
   <th id="wid1">Recordings</th>
</thead>
    <tbody>

<tr>
  @foreach($records as $record)
  <td id="wid2">{{$record->recording_id}}</td>
   <td id="wid2">{{$record->uniqueid}}</td>
  <td id="wid1">{{$record->phone_number}}</td>
    <td id="wid1">{{$record->datetime}}</td>
    <td id="wid1">{{$record->call_type}}</td>
    <td id="wid1">{{$record->extension}}</td> 
    <td id="wid1"><a class="btn btn-primary" id="next" href="/mp3/{{$record->recording_filename}}" 
      onclick="return playAudio();">Play</a>
      <a class="btn btn-success" id="" href="/mp3/{{$record->recording_filename}}" 
    >Donload</a>
      </td>
</tr>
    @endforeach
 </tbody>
</table>


//script for this

<script type="text/javascript">
var x = document.getElementById('next'); 
//alert(x);
function playAudio() {
x.play(); 
} 
</script>

Solution

  • Simple solution is you can use HTML <audio> tag.

      <audio controls>
      <source src="/mp3/{{$record->recording_filename}}" type="audio/mpeg"> 
      </audio>
    

    It will give option for Play/Pause and Download

    For download try this

      <a class="btn btn-success" href="/mp3/{{$record->recording_filename}}" download>Download</a>