Home  >  Q&A  >  body text

MediaStream in Safari fails to release resources correctly

<p>I am using the AudioWorkletNode method to record and all browsers seem to be recording fine. </p> <p>When I record, the recording icon appears on the tab and disappears when the recording ends. </p> <p> However, in Safari, a small speaker icon (usually indicating that the tab is emitting sound) will appear on the tab instead of the microphone icon. </p> <p>So I think I may not have closed something. </p> <p>After the recording is completed I will do the following:</p> <pre class="brush:php;toolbar:false;">//stream is a MediaStream type if(stream) stream.getTracks().forEach(track => track.stop()); stream = null</pre> <p>The stream is created via: </p> <pre class="brush:php;toolbar:false;">stream = await navigator.mediaDevices.getUserMedia({ audio: options });</pre> <p>Is there anything else needed to release all resources? </p>
P粉106715703P粉106715703437 days ago500

reply all(1)I'll reply

  • P粉536909186

    P粉5369091862023-08-31 10:10:19

    To turn everything off, the following method seems to work. I just found this through trial and error because I couldn't figure out how to get Safari to tell me what it still holds:

    // stream : MediaStream
    // sourceNode : MediaStreamAudioSourceNode
    // recorderNode : AudioWorkletNode
    
    const tracks = stream.getTracks();
    tracks.forEach((track) => {
      track.stop();
      stream.removeTrack(track)
    });
    stream = null
    
    sourceNode.disconnect();
    sourceNode = undefined
    
    recorderNode.disconnect();
    recorderNode = undefined

    reply
    0
  • Cancelreply