gpt4 book ai didi

react-native - react 本地YouTube观看事件

转载 作者:行者123 更新时间:2023-12-03 05:31:02 25 4
gpt4 key购买 nike

我想记录用户何时使用网络 View 对我嵌入到我的应用程序中的youTube视频进行播放。

当用户开始播放视频时,我似乎找不到找到通知的方法。

我的webView的JSX:

<WebView
javaScriptEnabled={true}
domStorageEnabled={true}
source={{ uri: "https://www.youtube.com/embed/" + videoId }}
/>

任何指针赞赏

最佳答案

对于那些将来会来到这里的人,我最终使用了从Webview拾取的html内发布的事件。

JSX:

<WebView
style={{
height: carouselHeight,
width: width
}}
javaScriptEnabled={true}
domStorageEnabled={true}
source={{
html: youtubeHTML(videoId, width, carouselHeight)
}}
onMessage={event => {
//see ./data/youtube.js to see how this event is built
if (event.nativeEvent.data === "videoEvent_0") {
/************ do whatever you want on the event *************/
}
}}
/>

youTubeHTML():
function youtubeHTML(
videoID: string,
width: number,
height: number
) {
//see https://medium.com/capriza-engineering/communicating-between-react-native-and-the-webview-ac14b8b8b91a
const returnValue =
`<!DOCTYPE html>
<html>
<head>

<meta name="viewport" content="initial-scale=1.0">
</head>
<body style="margin: 0px;background-color:#000;">
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>

<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '` +
height +
`',
width: '` +
width +
`',
videoId: '` +
videoID +
`',
events: {
'onStateChange': onPlayerStateChange
}
});
}

// 4. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
window.postMessage("videoEvent_"+JSON.stringify(event.data))
}
</script>
</body>
</html>`;
return returnValue;
}

关于react-native - react 本地YouTube观看事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49613263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com