Private Sub btnPlay_Click
If pnlMedia.Visible = False Then pnlMedia.SetVisibleAnimated(250, True)
pnlParent.SetVisibleAnimated(100, False)
Dim p As Phone
p.SetScreenOrientation(0)
Dim html As String = $"<!DOCTYPE html>
<html style="height: 100%; margin: 0;">
// <body style="height: 100%; margin: 0;">
<body>
<div id="player" style="height: 100%; width: 100%;"></div>
<script>
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
try {
player = new YT.Player('player', {
height: '100%',
width: '100%',
videoId: '${videoID}',
playerVars: {
'autoplay': 1,
'mute': 1, // Start muted to bypass autoplay restriction
'controls': 1,
'playsinline': 1
},
events: {
'onReady': onPlayerReady
}
});
} catch (error) {
handleError("Error initializing YouTube player", error);
}
}
function onPlayerReady(event) {
B4X.CallSub("Player_Ready", true);
}
function playVideo() {
try {
player.playVideo();
B4X.CallSub("Video_Playing", true);
} catch (error) {
handleError("Error in onPlayerReady", error);
}
}
// This function will stop the video when called from B4X
function stopVideo() {
player.stopVideo();
}
function unMuteVideo() {
try {
player.unMute();
B4X.CallSub("Video_unMuted", true);
} catch (error) {
B4X.CallSub("Error_Message", true);
handleError("Error unmuting video", error);
}
}
function getPlayerDimensions() {
var playerElement = document.getElementById('player');
var height = playerElement.offsetHeight;
var width = playerElement.offsetWidth;
callB4aSub('Player_Dimensions', { Dimensions: [height, width] } )
}
function callB4aSub(b4xSubName, values) {
setTimeout(function() {
if (!values) {
return B4X.CallSub('CallB4xUiSub_Async', true, b4xSubName, "");
}
return B4X.CallSub('CallB4xUiSub_Async', true, b4xSubName, JSON.stringify(values));
}, 0);
}
// Global error handler
window.addEventListener("error", function (event) {
handleError("Global Error", event.error || event.message);
});
// Error handler to log and pass errors to B4X
function handleError(context, error) {
var errorMessage = context + ": " + (error.message || error);
console.error(errorMessage);
callB4aSub('Handle_JavascriptError', { error: [context, error] });
// Pass error to B4X
try {
callB4aSub('Handle_JavascriptError', { error: [context, error.message] });
} catch (innerError) {
console.error("Error in handleError: ", innerError);
}
}
</script>
</body>
</html>
"$
WebView1.JavaScriptEnabled = True
wbe.addWebChromeClient(WebView1, "webviewclient")
wbs.setAppCacheEnabled(WebView1, True)
wbs.setDefaultZoom(WebView1, "FAR")
wbs.setDOMStorageEnabled(WebView1, True)
wbs.setLoadWithOverviewMode(WebView1, False)
wbe.AddJavascriptInterface(WebView1, "B4X")
'''WebView1.LoadUrl("https://drive.google.com/file/d/1vFJlzi5GdJUNiyv8J1vXndrV9oIeAQdv/preview")
WebView1.LoadHtml(html)
End Sub