我正在使用WebView2浏览器允许播放一些YouTube视频,但全屏选项不可用。
我写的代码在下面。我对编码很陌生,我可能犯了很大的错误。
你需要的任何信息我都愿意提供。
public partial class HowToViewSites: CMBaseDialog
{
private dynamic player;
public HowToViewSites()
{
InitializeComponent();
InitializeWebView2();
}
private async void InitializeWebView2()
{
await webView21.EnsureCoreWebView2Async(null);
webView21.CoreWebView2.Settings.IsScriptEnabled = true;
webView21.CoreWebView2InitializationCompleted += WebView2_CoreWebView2InitializationCompleted;
string htmlContent = @"
<html>
<head>
<!-- Include the YouTube iframe API script using HTTPS -->
<script src='https://www.youtube.com/iframe_api'></script>
</head>
<body>
<!-- Embed the YouTube video with enablejsapi parameter over HTTPS -->
<iframe id='player' type='text/html' width='840' height='560'
src='https://www.youtube.com/embed/QgMnE1No_6A?enablejsapi=1'
frameborder='0'></iframe>
<!-- JavaScript code to handle fullscreen changes -->
<script>
// Initialize the YouTube iframe API when the script is loaded
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
// Player is ready
// You can control playback and perform other actions here
}
function onPlayerStateChange(event) {
// Player state has changed (e.g., video started, paused, etc.)
// Check if the player is in fullscreen mode
var isFullscreen = document.fullscreenElement === player.getIframe();
if (isFullscreen) {
// Trigger the player's native fullscreen mode
external.RequestFullscreen();
} else {
// Exit fullscreen
external.ExitFullscreen();
}
}
document.addEventListener('fullscreenchange', function () {
console.log('Fullscreen change event triggered');
window.chrome.webview.postMessage('fullscreenchange');
});
</script>
</body>
</html>
";
webView21.NavigateToString(htmlContent);
webView21.CoreWebView2.Settings.IsWebMessageEnabled = true;
webView21.WebMessageReceived += CoreWebView2_WebMessageReceived;
}
private async void WebView2_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (e.IsSuccess)
{
// No need to manually call onYouTubeIframeAPIReady here
}
else
{
MessageBox.Show("WebView2 initialization failed.");
}
}
private void CoreWebView2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
{
string message = e.TryGetWebMessageAsString();
if (message == "enteredFullscreen")
{
RequestFullscreen();
}
else if (message == "exitedFullscreen")
{
ExitFullscreen();
}
}
public void RequestFullscreen()
{
this.BeginInvoke(new Action(() =>
{
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.TopMost = true;
player.playVideo();
}));
}
public void ExitFullscreen()
{
this.BeginInvoke(new Action(() =>
{
this.FormBorderStyle = FormBorderStyle.Sizable;
this.WindowState = FormWindowState.Normal;
this.TopMost = false;
player.pauseVideo();
}));
}
}
非常感谢您抽出时间来帮助我。
1条答案
按热度按时间7gcisfzg1#
几个注意事项:
InitializeWebView2()
方法返回Task
。您可以覆盖
OnLoad()
方法(或订阅Load
事件),使其成为“,并在此等待InitializeWebView2()
.EnsureCoreWebView2Async()
之前,需要订阅[WebView2].CoreWebView2InitializationComplete我建议不要直接使用IFrame来嵌入视频。
YouTube播放器已经具备以下功能:它可以根据传递给其构造函数的ID,用IFrame元素替换元素,IFrame元素使用创建播放器时设置的属性进行配置。
这允许例如停止/暂停/恢复回放调用JavaScript函数。你可以使用ExecuteScriptAsync()方法。
您也可以在播放器的
playerVars
属性中设置enablejsapi = 1
。例如,这允许复制/ * 获取 * 视频的地址:
只使用它的
videoId
-QgMnE1No_6A
-来初始化播放器这也会启用播放器的全屏按钮。
如果你使用IFrame,你必须设置一个属性:
webView
(没有原因,它就是这样:)*我已经更改了HTML和JavaScript,因此您可以使用Video ID和指定Size的页面初始化播放器:
现在,如果你想与播放器交互,只需调用ExecuteScriptAsync()来执行你设置的函数。现在,您可以开始/停止/恢复投资回报: