chrisguttandin/extendable-media-recorder

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

Closed this issue · 2 comments

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?

@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())
      }

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);