Webview2动态设置页面video的Blob进行播放
Webview2动态设置页面video的Blob进行播放
<Window
x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp2"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:webview2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="720" Width="1280">
<Grid>
<webview2:WebView2 x:Name="webview"
Source="https://www.baidu.com" Margin="0,272,0,0"></webview2:WebView2>
<Button VerticalAlignment="Top" Height="148" Margin="235,0,579,0" Click="Button_Click">Play</Button>
</Grid>
</Window>
public MainWindow()
{
InitializeComponent();
webview.Source = new Uri("http://localhost/play.html");
}
void testPlay() {
// 读取视频文件为字节数组
byte[] videoData = File.ReadAllBytes("111.MP4");
// 将字节数组转换为 Base64 字符串
string base64Video = Convert.ToBase64String(videoData);
// 构建 Blob URL
string script = $@"
var byteCharacters = atob('{base64Video}');
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {{
byteNumbers[i] = byteCharacters.charCodeAt(i);
}}
var byteArray = new Uint8Array(byteNumbers);
var blob = new Blob([byteArray], {{ type: 'video/mp4' }});
var video = document.getElementById('myVideo');
video.src = URL.createObjectURL(blob);
video.play();
";
// 执行 JavaScript
webview.ExecuteScriptAsync(script);
}
fffffffffffffffff
test red font.

浙公网安备 33010602011771号