chrisguttandin / extendable-media-recorder

An extendable drop-in replacement for the native MediaRecorder.
MIT License
258 stars 13 forks source link

Set sample rate on Safari doesn't work as expected. #689

Closed dongzeli95 closed 4 months ago

dongzeli95 commented 4 months ago
const audioContext = new AudioContext({ sampleRate: 16000 });
const mediaStreamAudioSourceNode = new MediaStreamAudioSourceNode(audioContext, { mediaStream: stream });
const mediaStreamAudioDestinationNode = new MediaStreamAudioDestinationNode(audioContext);

mediaStreamAudioSourceNode.connect(mediaStreamAudioDestinationNode);

const mediaRecorder = new MediaRecorder(mediaStreamAudioDestinationNode.stream);

Hi team, I tried to set this sample rate to 16000 and it worked perfect on Chrome, but doesn't work on Safari. When I use my laptop speaker, the default sample rate is 48000, on safari this won't get changed even though I set the audio context. Any ideas?

668 I suppose it's related to some inner workings of safari?

bschelling commented 4 months ago

@dongzeli95 did you try to check if the native mediarecorder can handle the sample rate restriction? You could check the capabilities first (doesn't work in Firefox though)

  const audioTrack = this.mediaStream.getAudioTracks()[0]
      if (typeof audioTrack.getCapabilities === 'function') {
        console.log('capabilities audio', audioTrack.getCapabilities())
      }
chrisguttandin commented 4 months ago

Yes, @bschelling is on the right track. Safari doesn't expose the sampleRate of a MediaStreamTrack. It needs a little help.

const { stream } = mediaStreamAudioDestinationNode;

Object.defineProperty(
    stream.getAudioTracks()[0],
    'getSettings',
    { value: () => ({ sampleRate: audioContext.sampleRate }) }
);

const mediaRecorder = new MediaRecorder(stream);