周末加个班,怼了一项新需求,只是没想到转了产品又来搞研发,那就好好搞吧。
公司要做一个屏幕管控系统,需要技术用winform +cefsharp 播放MP3 MP4 以及flash
网上看了很多,不管是什么有偿,50积分啊,总体看来还是有解决方式的,下面说一下大概思路然后再分享具体步骤
首先大家都知道 cefsharp 是不支持MP3MP4的播放的,所以 要对cefsharp进行重新打包,具体有多麻烦,网上一看就一大堆
解决办法:用大佬二次打包出来的各种 cef_binary_3.2623.1401包
下面进入正题:
首先VS上下载NuGet库
下载完以后 右键点击项目 选择NuGet库
搜索cefsharp就可以收到 然后下载这四个鬼
让你下载完以后 你的目录下多了packages
因为我是64位机器所以 我选择64位的包
现在基本上就已经完成一半了然后
创建一个winform 项目
在默认窗口脚本Form1中
using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace lmqBrowser2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CefExample.Init();//初始化内核
}
public static ChromiumWebBrowser browser;
private void Form1_Load(object sender, EventArgs e)
{
// if (this.WindowState == FormWindowState.Maximized) //如果当前的窗体是最大化
// {
// this.FormBorderStyle = FormBorderStyle.Fixed3D; //固定的三围边框
// this.WindowState = FormWindowState.Normal; //把当前窗体还原默认大小
// }
// else
// {
//this.FormBorderStyle = FormBorderStyle.None; //将该窗体的边框设置为无
//this.WindowState = FormWindowState.Maximized; //将该窗体设置为最大
// }
//this.TopMost = true; //置顶
InitBrowser();//启动浏览器
}
public void InitBrowser()
{
//var setting = new CefSettings();
/*
setting.Locale = "zh-CN";
//缓存路径
setting.CachePath = "C:\\temp\\BrowserCache";
//浏览器引擎的语言
setting.AcceptLanguageList = "zh-CN,zh;q=0.8";
setting.LocalesDirPath = "C:\\temp\\localeDir";
//日志文件
setting.LogFile = "C:\\temp\\LogData";
setting.PersistSessionCookies = true;
setting.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
setting.UserDataPath = "C:\\temp\\userData";
//设置flash版本
*/
//setting.CefCommandLineArgs.Add("ppapi-flash-path", System.AppDomain.CurrentDomain.BaseDirectory + "plugins\\pepflashplayer.dll"); //指定的flash版本,不要使用系统安装的flash版本
//setting.CefCommandLineArgs.Add("ppapi-flash-version", "32_29_0_0_113");
//setting.CefCommandLineArgs.Add("ppapi-flash-version", "27_0_0_130");//PepperFlash\manifest.json中的version
//setting.CefCommandLineArgs.Add("ppapi-flash-path", "plugins\\pepflashplayer.dll");
//CefSharp.Cef.Initialize(setting);
//browser = new ChromiumWebBrowser("chrome://version");//"http://192.168.56.1:8083/html/14/index.html"
//browser = new ChromiumWebBrowser("https://www.bilibili.com/video/av26238012/?spm_id_from=333.334.chief_recommend.21");
//browser = new ChromiumWebBrowser("http://58.87.107.253/testfintech/56dddf2de99ea8e91d097c107df9ddf7.mp4");
browser = new ChromiumWebBrowser("http://ssfw.njfy.gov.cn/ssfwzx/ext/flash/flash.jsp");
//browser.Load("chrome://version");
this.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
}
}
在Form1.Designer.cs中
namespace lmqBrowser2
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
}
}
还有一个是支持flash 的脚本 CefExample
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CefSharp;
namespace lmqBrowser2
{
public static class CefExample
{
public const string DefaultUrl = "http://www.google.com/";
private static readonly bool DebuggingSubProcess = Debugger.IsAttached;
public static void Init()
{
var settings = new CefSettings();
settings.RemoteDebuggingPort = 8088;
settings.Locale = "zh-CN";
settings.CefCommandLineArgs.Add("ppapi-flash-path", System.AppDomain.CurrentDomain.BaseDirectory + "pepflashplayer.dll"); //指定flash的版本,不使用系统安装的flash版本
settings.CefCommandLineArgs.Add("ppapi-flash-version", "29.0.0.171");
settings.CefCommandLineArgs.Add("enable-media-stream", "enable-media-stream");
settings.IgnoreCertificateErrors = true;
settings.LogSeverity = LogSeverity.Verbose;
if (DebuggingSubProcess)
{
//var architecture = Environment.Is64BitProcess ? "x64" : "x86";
//settings.BrowserSubprocessPath = "..\\..\\..\\..\\CefSharp.BrowserSubprocess\\bin\\" + architecture + "\\Debug\\CefSharp.BrowserSubprocess.exe";
}
//settings.RegisterScheme(new CefCustomScheme
//{
// SchemeName = CefSharpSchemeHandlerFactory.SchemeName,
// SchemeHandlerFactory = new CefSharpSchemeHandlerFactory()
//});
if (!Cef.Initialize(settings))
{
if (Environment.GetCommandLineArgs().Contains("--type=renderer"))
{
Environment.Exit(0);
}
else
{
return;
}
}
}
}
}
因为是一个测试所以,program中都是默认,没有发生任何改变
先别着急编译,下面是项目中一些细节,cefsharp 不支持anycpu 所以只有32位和64位 这要先选好
最后还有一点 为了支持flash 要把 pepflashplayer.dll 放到 你的当前项目\bin\x64\Debug 下
最后我附上 大佬们的二次编译的包
以及 pepflashplayer.dll
pepflashplayer.dll下载地址:https://download.youkuaiyun.com/download/ldy597321444/10821710
64位包:https://download.youkuaiyun.com/download/ldy597321444/10821756
32位包:https://download.youkuaiyun.com/download/ldy597321444/10821757