| <!DOCTYPE html> |
| <html> |
| <head> |
| <title>MediaElementAudioSourceNode: pausing the underlying media element stops audio output</title> |
| <link rel="help" href="https://webaudio.github.io/web-audio-api/#MediaElementAudioSourceNode"> |
| <script src="../../imported/w3c/web-platform-tests/resources/testharness.js"></script> |
| <script src="../../resources/testharnessreport.js"></script> |
| </head> |
| <body> |
| <script> |
| "use strict"; |
| |
| function waitFor(element, type) { |
| return new Promise(resolve => element.addEventListener(type, resolve, { once: true })); |
| } |
| |
| function sleepFor(ms) { |
| return new Promise(resolve => setTimeout(resolve, ms)); |
| } |
| |
| promise_test(async t => { |
| const audio = new Audio("../resources/media/sine440.mp3"); |
| waitFor(audio, "error").then(event => { |
| throw new Error(`audio element error: code=${event.target.error.code}, message="${event.target.error.message}"`); |
| }); |
| |
| const context = new AudioContext({sampleRate: 48000}); |
| t.add_cleanup(() => context.close()); |
| |
| const source = context.createMediaElementSource(audio); |
| const analyser = context.createAnalyser(); |
| analyser.fftSize = 2048; |
| analyser.smoothingTimeConstant = 0; |
| analyser.minDecibels = -40; |
| analyser.maxDecibels = 0; |
| |
| const gain = context.createGain(); |
| gain.gain.value = 1; |
| |
| source.connect(analyser); |
| analyser.connect(gain); |
| gain.connect(context.destination); |
| |
| context.resume(); |
| audio.play(); |
| |
| await t.step_wait(() => audio.currentTime >= 0.2, "currentTime progressed to >= 0.2s."); |
| |
| const binIndex = Math.round(440 * analyser.fftSize / context.sampleRate); |
| const buf = new Uint8Array(analyser.frequencyBinCount); |
| |
| await t.step_wait(() => { |
| analyser.getByteFrequencyData(buf); |
| return buf[binIndex] > 0; |
| }, "440Hz bin is non-silent while playing."); |
| assert_false(audio.paused, "audio.paused is false while playing."); |
| audio.pause(); |
| assert_true(audio.paused, "audio.paused is true immediately after pause()."); |
| |
| await t.step_wait(() => { |
| analyser.getByteFrequencyData(buf); |
| return buf[binIndex] > 0; |
| }, "440Hz bin is silent shortly after element is paused.", 200, 50); |
| }, "Pausing the underlying HTMLMediaElement stops audio production through MediaElementAudioSourceNode and the node has no tail time"); |
| </script> |
| </body> |
| </html> |