1

I created a software that record the voice and create the Blob:

blob = new Blob(chunks, { 'type' : 'audio/mp3; codecs=opus' });

where chunks are

mediaRecorder.ondataavailable = function(e) {
     chunks.push(e.data);
}

Then with ajax, I upload the blob with formData to a Codeigniter server:

var formData = new FormData();
formData.append("s_registra", blob);
formData.append('<?= csrf_token() ?>', '<?= csrf_hash() ?>');

$.ajax({
   url: '<?php echo site_url('/accetta') ?>',
   type: 'POST',
   data: formData,
   processData: false,
   contentType: false,
   error: function(xhr,status,error) {
        var err = eval("(" + xhr.responseText + ")");
        console.log(err.Message);
   },
   success: function(data) {
        console.log(data);
   }
});

In the controller (codeigniter 4.3.6) on the server I use this code to save the blob into a mp3 file. But when you try to play the mp3, it doesn't work. Where are the problems?

$current = file_get_contents($this->request->getFile('s_registra'));
$ritorno = file_put_contents(FCPATH . 'uploads/blob/prova.mp3' , $current);
TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 2
    What makes you think the data is recorded in mp3 format? As far as I can tell from a quick google, the media recorder API in JS records in webm format (or maybe ogg, if you use Firefox). If you want to turn that into an mp3 file, you'd need to re-encode it when it reaches the server (perhaps something like ffmpeg could do that for, for example). Background reading: https://stackoverflow.com/questions/41739837/all-mime-types-supported-by-mediarecorder-in-firefox-and-chrome – ADyson Aug 31 '23 at 13:12

0 Answers0