I used react-native-webview to open the webpage, but the audio doesn't automatically stream/play until I press the button. Is there a way I can open the page and have it directly play or can I add a custom that plays the radio/audio on click?
import React from 'react'
import { View } from 'react-native'
import { WebView } from 'react-native-webview'
export default function Test() {
const jsCode = "setTimeout(function() { const playButton = document.getElementsByClassName('jp-play')[0]; playButton.click();}, 2000)";
return (
<View style={{ flex: 1 }}>
<WebView
javaScriptEnabled={true}
injectedJavaScript={jsCode}
source={{ uri: 'https://duckduckgo.com/?q=google&ia=web' }}
/>
</View>
)
}
You can pass a javascript code into the WebView component to achieve that.
First, you need to wait some seconds for loading the web view, then get the play button element by its id/className or any query selector you want.
since your play button has a jp-play className, you can use it to select this play button and then click on it:
let's implement:
const jsCode = "setTimeout(function() { const playButton = document.getElementsByClassName('jp-play')[0]; playButton.click();}, 2000)";
<WebViw
source={{ uri: 'https://mmufmkenya.radio12345.com/' }}
javaScriptEnabled={true} // ---> don't forget this
injectedJavaScript={jsCode}
/>