birdofpreyru / react-native-static-server

Embedded HTTP server for React Native
https://dr.pogodin.studio/docs/react-native-static-server
Other
135 stars 21 forks source link

Issues with navigator.mediaDevices in static services #113

Closed zhaochg closed 1 month ago

zhaochg commented 1 month ago
// start server
const server= new Server({
    // ...
    port: 8088,
    stopInBackground: true,
  });

 // Use webview to load html files, using 'mediaDevices' method
navigator.mediaDevices
  .getUserMedia(constraints)
  .then((stream) => {
    /* use the stream */
  })
  .catch((err) => {
    /* handle the error */
  });

An error (DOMException: Permission denied) occurred in this code. Is there any solution? @birdofpreyru

birdofpreyru commented 1 month ago

I don't see how navigator.mediaDevices is it related to the server? I guess, it is purely a WebView problem.

zhaochg commented 1 month ago

I don't see how navigator.mediaDevices is it related to the server? I guess, it is purely a WebView problem.

navigator.mediaDevices must be used in https environment or localhost (127.0.0.1). After react-native-static-server is started, although 127.0.0.1 is used to access the html file, the navigator.mediaDevices method cannot be used.


//This is the configuration of webview
<WebView
    style={{ flex: 1 }}
    source={{
        uri: `xxxx`,
    }}
    onMessage={handleReceiveMessage}
    javaScriptEnabled={true}
    domStorageEnabled={true}
    mediaPlaybackRequiresUserAction={false}
    allowUniversalAccessFromFileURLs={true}
    allowsInlineMediaPlayback={true}
    originWhitelist={['*']}
    allowFileAccess={true}
    mixedContentMode="always"
    allowsProtectedMedia={true}
    allowFileAccessFromFileURLs={true}
/>
birdofpreyru commented 1 month ago

But it is WebView who decides whether it allows you to use navigator.mediaDevices or not, I still don't see any influence of the server on its decision ¯\_(ツ)_/¯