diff --git a/examples/README.md b/examples/README.md
index d933cdbb2..e7ecad272 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -5,3 +5,18 @@ You can run them in terminal like this:
```sh
mvn compile exec:java -Dexec.mainClass=org.example.PageScreenshot
```
+
+## Fake microphone input
+
+[`FakeMicrophoneInput`](./src/main/java/org/example/FakeMicrophoneInput.java) feeds a WAV file to the page as microphone input, so a page that records audio can be tested without a microphone. Without an argument it generates a tone; pass a WAV file, such as a recorded spoken phrase, to use that instead:
+
+```sh
+mvn compile exec:java -Dexec.mainClass=org.example.FakeMicrophoneInput
+mvn compile exec:java -Dexec.mainClass=org.example.FakeMicrophoneInput -Dexec.args=/path/to/phrase.wav
+```
+
+- It works in Chromium only, with the `--use-fake-ui-for-media-stream`, `--use-fake-device-for-media-stream` and `--use-file-for-fake-audio-capture` switches.
+- Use a 16-bit PCM WAV file. Chromium reads it at launch and plays it in a loop, so a different file needs a new browser.
+- The page must be a secure context, such as `https://` or `http://localhost`, for `getUserMedia` to be available. The example serves its page at `http://localhost/` with `page.route`.
+- It feeds pages that capture audio with `getUserMedia` (MediaRecorder, WebRTC, Web Audio). It does not feed `webkitSpeechRecognition`, which listens to the system microphone.
+- Playwright mutes Chromium by default. To hear the input while debugging, launch with `setIgnoreDefaultArgs(asList("--mute-audio"))` and connect the audio to `AudioContext.destination` in the page.
diff --git a/examples/src/main/java/org/example/FakeMicrophoneInput.java b/examples/src/main/java/org/example/FakeMicrophoneInput.java
new file mode 100644
index 000000000..a03caaee5
--- /dev/null
+++ b/examples/src/main/java/org/example/FakeMicrophoneInput.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) Microsoft Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.example;
+
+import com.microsoft.playwright.*;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import static java.util.Arrays.asList;
+
+// Feeds a WAV file to the page as microphone input: the file given as the first
+// argument, or a generated tone. Chromium only: the file is
+// read at launch and loops, so a different clip needs a new browser. Works for
+// pages that capture with getUserMedia (MediaRecorder, WebRTC, Web Audio), not
+// for webkitSpeechRecognition, which listens to the real microphone.
+public class FakeMicrophoneInput {
+ private static final int SAMPLE_RATE = 16000;
+
+ public static void main(String[] args) throws IOException {
+ boolean generated = args.length == 0;
+ Path wav = generated ? Files.createTempFile("fake-mic", ".wav") : Paths.get(args[0]);
+ try {
+ if (generated) {
+ writeTone(wav, 440, 1000, 2000, 1000);
+ }
+ try (Playwright playwright = Playwright.create()) {
+ Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setArgs(asList(
+ "--use-fake-ui-for-media-stream",
+ "--use-fake-device-for-media-stream",
+ "--use-file-for-fake-audio-capture=" + wav.toAbsolutePath())));
+ BrowserContext context = browser.newContext(new Browser.NewContextOptions()
+ .setPermissions(asList("microphone")));
+ Page page = context.newPage();
+ page.route("http://localhost/", route -> route.fulfill(new Route.FulfillOptions().setContentType("text/html").setBody(
+ "\n" +
+ "\n" +
+ "")));
+ page.navigate("http://localhost/");
+ page.getByText("Listen").click();
+ page.waitForFunction("() => window.peak > 0.1");
+ System.out.println("Microphone input received, peak level: " + page.evaluate("window.peak"));
+ browser.close();
+ }
+ } finally {
+ if (generated) {
+ Files.deleteIfExists(wav);
+ }
+ }
+ }
+
+ private static void writeTone(Path path, int frequency, int leadMs, int toneMs, int trailMs) throws IOException {
+ int lead = SAMPLE_RATE * leadMs / 1000;
+ int tone = SAMPLE_RATE * toneMs / 1000;
+ int total = lead + tone + SAMPLE_RATE * trailMs / 1000;
+ ByteBuffer buffer = ByteBuffer.allocate(44 + total * 2).order(ByteOrder.LITTLE_ENDIAN);
+ buffer.put("RIFF".getBytes()).putInt(36 + total * 2).put("WAVE".getBytes());
+ buffer.put("fmt ".getBytes()).putInt(16).putShort((short) 1).putShort((short) 1)
+ .putInt(SAMPLE_RATE).putInt(SAMPLE_RATE * 2).putShort((short) 2).putShort((short) 16);
+ buffer.put("data".getBytes()).putInt(total * 2);
+ for (int i = 0; i < total; i++) {
+ boolean inTone = i >= lead && i < lead + tone;
+ double sample = inTone ? 0.5 * Math.sin(2 * Math.PI * frequency * i / SAMPLE_RATE) : 0;
+ buffer.putShort((short) (sample * Short.MAX_VALUE));
+ }
+ Files.write(path, buffer.array());
+ }
+}