'Why html player doesnt play audio blob on IOS safari?

So I have two buttons(start/stop), canvas with sound visualizer and html5 audio player. Everything works cool, but not on IOS Safari (tested on 15.3). On IOS it shows that recording was started, that everything is going ok, but when the recording has stopped and blob inserted into audio tag, it shows "error" message inside audio-tag instead of playing this blob. Here is the code:

        var stop = document.querySelector('#rec-stop');        
        var player = document.querySelector('#rec-player'); 
        var totest = document.querySelector('.test-choice-btn'); 
        navigator.mediaDevices.getUserMedia({audio:true})
          .then(stream => {handlerFunction2(stream)})  
          function handlerFunction2(stream) {
            rec = new MediaRecorder(stream);
            audioContext = new AudioContext();
            analyser = audioContext.createAnalyser();
            microphone = audioContext.createMediaStreamSource(stream);
            javascriptNode = audioContext.createScriptProcessor(2048, 1, 1);

            analyser.smoothingTimeConstant = 0.8;
            analyser.fftSize = 1024;

            microphone.connect(analyser);
            analyser.connect(javascriptNode);
            javascriptNode.connect(audioContext.destination);

            canvasContext = $("#rec-canvas")[0].getContext("2d");

            javascriptNode.onaudioprocess = function() {
                var array = new Uint8Array(analyser.frequencyBinCount);
                analyser.getByteFrequencyData(array);
                var values = 0;

                var length = array.length;
                for (var i = 0; i < length; i++) {
                    values += (array[i]);
                }

                var average = values / length;    

                canvasContext.clearRect(0, 0, 150, 270);
                canvasContext.fillStyle = '#BadA55';
                canvasContext.fillRect(0, 270 - average, 150, 270);
                canvasContext.fillStyle = '#262626';            
            }      
            audioChunks = [];
            rec.start();       
            stop.onclick = e => {                               
                rec.stop();   
                totest.classList.add('shown');             
            } 
            
            rec.ondataavailable = e => {
                audioChunks.push(e.data);
                if (rec.state == "inactive"){
                    let blob = new Blob(audioChunks,{type:'audio/mpeg-3'});
                    player.src = URL.createObjectURL(blob);
                    player.controls=true;
                    player.autoplay=true;                
                }
            }
        } 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source