弹来弹去跑马灯!

Webview2动态设置页面video的Blob进行播放

Webview2动态设置页面video的Blob进行播放

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<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>

  

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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);
 
 
 
 
 
 }

  

posted @   wgscd  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2019-01-14 WPF中ListBox /ListView如何改变选中条背景颜色
2019-01-14 c# C#获取屏幕鼠标坐标点颜色
2019-01-14 c# 无边框窗体的边框阴影
点击右上角即可分享
微信分享提示