Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webview Support (React Native) #1319

Open
ttm opened this issue Feb 19, 2025 · 0 comments
Open

Webview Support (React Native) #1319

ttm opened this issue Feb 19, 2025 · 0 comments

Comments

@ttm
Copy link

ttm commented Feb 19, 2025

We are trying to make Tone.js work in React Native using a webview:
https://github.com/react-native-webview/react-native-webview

Currently, it makes no sound in iOS, but it makes some distorted sound in the Android emulator,
and it doesn't crash the system in either iOS and Android.

We are considering using Kotlin and Swift to make audio work in React Native for us,
if we can't make Tone.js work in the webview.
Has anyone successfully used Tone.js in RN/Webview?
Does anyone have advices or want to guide us in making Tone.js work in the React Native's webview?

We are using this component for testing:

const WebviewTest = () => {
  const triggerSound = () => {
    webviewRef.current.injectJavaScript(`playTone();`);
  };

  const webviewRef = useRef(null);

  return (
    <View style={styles.container}>
      <Button title="Play Sound" onPress={triggerSound} />
      <WebView
        ref={webviewRef}
        source={{
          html: `
          <html>
          <head>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/tone/15.0.4/Tone.js"></script>
          </head>
          <body style="display:flex; justify-content:center; align-items:center; height:100vh; background:#f5f5f5;">
            <button onclick="playSound()">Play Sound</button>
            <script>
              function playSound() {
                console.log("🎵 Button pressed - Initializing Tone.js...");
                const synth = new Tone.Synth().toDestination();
                Tone.start().then(() => {
                  console.log("🔊 Tone.js Audio Context Started");
                  synth.triggerAttackRelease("C4", "2");
                }).catch(error => {
                  console.error("⚠️ Tone.js failed to start:", error);
                });
              }
            </script>
          </body>
          </html>
          `,
        }}
        style={styles.webview}
      />
    </View>
  );
};

export default WebviewTest;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant