Electron flash插件

Electron 支持 Pepper Flash 插件。要在 Electron 里面使用 Pepper Flash 插件,我们 要手动设置 Pepper Flash 的路径并且应用里启用 Pepper Flash。

我们可以在chrome浏览器中输入:chrome://version/来获取浏览器的信息

其中flash信息就是我们想要的。
当然了,大家也可 以去flash官网下载安装flash,安装后的目录在:C:\Windows\SysWOW64\Macromed\Flash下面。

接下来我们就开始看看怎么在electron项目中使用。这是我将pepflashplayer.dll拷贝到我工程后的目录结构(Pepper Flash 插件的操作系统必须和 Electron 的操作系统匹配。在 Windows 中, 一个常见的错误是对64位版本的 Electron 使用 32bit 版本的 Flash 插件)


在electron的官方文档中我们可以找到设置插件的方式:

const { app, BrowserWindow } = require('electron')
const path = require('path')

// 指定 flash 路径,假定它与 main.js 放在同一目录中。
let pluginName
switch (process.platform) {
  case 'win32':
    pluginName = 'pepflashplayer.dll'
    break
  case 'darwin':
    pluginName = 'PepperFlashPlayer.plugin'
    break
  case 'linux':
    pluginName = 'libpepflashplayer.so'
    break
}
app.commandLine.appendSwitch('ppapi-flash-path', path.join(__dirname, pluginName))

// 可选:指定 flash 的版本,例如 v17.0.0.169
app.commandLine.appendSwitch('ppapi-flash-version', '17.0.0.169')

app.on('ready', () => {
  let win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      plugins: true
    }
  })
  win.loadURL(`file://${__dirname}/index.html`)

})
您也可以尝试加载系统安装的 Pepper Flash 插件,而不是装运 插件,其路径可以通过调用 app.getPath('pepperFlashSystemPlugin') 获取。
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
这是我项目的配置:

const {resolve} = require('path');

let plugins = resolve(__dirname, '../../lib/pepflashplayer64.dll')
if (__dirname.includes(".asar")) {
    plugins = require('path').join(process.resourcesPath + '/lib/pepflashplayer64.dll')
}
app.commandLine.appendSwitch('ppapi-flash-path', plugins)
1
2
3
4
5
6
7
之所以有__dirname.includes(".asar")这个判断,是因为开发环境和打包后的目录结构是不一样的。这一点大家可以在找一下打包相关的资料就知道了。

到了这里我们运行程序会发现,当我们播放一个视频的时候,仍然无法播放。此时:
如果使用的是webview:
在 标签里添加 plugins 属性。

<webview src="https://www.adobe.com/software/flash/about/" plugins></webview>
1
如果使用的是BrowserWindow:

  let win = new BrowserWindow({
        width: 800,
        height: 600,
        frame: true,
        autoHideMenuBar: true,
        maximizable: true,
        minimizable: true,
        transparent: true,
        'webPreferences': { // 注意一定要 添加这个 不然flash插件不会生效
            plugins: true,
            webSecurity: false
        }
    })
1
2
3
4
5
6
7
8
9
10
11
12
13
plugins: true 这个配置项是必须的。这个选项会告诉electron我们要使用插件,给个方便。。

到了这里我们在本地运行项目就不会有问题了,接下来我们就要配置一下打包的相关参数了。

在pakage.json中,我们build的配置下面内容:

"build": {
    "productName": "产品名称",
    "appId": "产品id",
    "directories": {
    //输出目录
      "output": "build" 
    },
    "files": [
      "dist/electron/**/*"
    ],
    "asar": true,
    "artifactName": "${productName}-${version}.${ext}",
    "compression": "maximum",
    "nsis": {
      "allowToChangeInstallationDirectory": true,
      "oneClick": false,
      "menuCategory": true,
      "allowElevation": false
    },
    "electronDownload": {
      "mirror": "http://npm.taobao.org/mirrors/electron/"
    },
    "dmg": {
      "contents": [
        {
          "x": 410,
          "y": 150,
          "type": "link",
          "path": "/Applications"
        },
        {
          "x": 130,
          "y": 150,
          "type": "file"
        }
      ]
    },
    "mac": {
      "icon": "build/icons/icon.icns"
    },
    "win": {
      "icon": "build/icons/icon.ico",
      "target": "nsis",
      //将特定的文件排除,不打包在asar包内
      "extraResources": "./lib/*.dll"
    },
    "linux": {
      "icon": "build/icons"
    }
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
40
41
42
43
44
45
46
47
48
49
其中最重要的配置就是 "extraResources": "./lib/*.dll" 这样的话我们的所有配置就算完成了,然后运行build,

yarn run build
1
打包完成后的目录如下:


其中exe就是我们的安装程序了。

我们的插件就在打包完成后的这个目录下,如果你运行安装程序,安装完成后的目录结构和win-unpacked目录结构是一致的。
--------------------- 
作者:秋殇阁 
来源:CSDN 
原文:https://blog.csdn.net/qingyulove/article/details/84582105 
版权声明:本文为博主原创文章,转载请附上博文链接!

你可以使用 Electron 的 `desktopCapturer` API 来进行截图。这个 API 允许你从用户的显示器中捕获屏幕、窗口或标签页的内容。以下是一个简单的例子: ```javascript const { desktopCapturer } = require('electron') desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => { for (const source of sources) { if (source.name === 'Entire Screen' || source.name === 'Screen 1') { try { const stream = await navigator.mediaDevices.getUserMedia({ audio: false, video: { mandatory: { chromeMediaSource: 'desktop', chromeMediaSourceId: source.id, minWidth: 1280, maxWidth: 1280, minHeight: 720, maxHeight: 720 } } }) handleStream(stream) } catch (e) { handleError(e) } return } } }) function handleStream (stream) { const video = document.createElement('video') video.srcObject = stream video.onloadedmetadata = () => { video.play() const canvas = document.createElement('canvas') canvas.width = video.videoWidth canvas.height = video.videoHeight const ctx = canvas.getContext('2d') ctx.drawImage(video, 0, 0, canvas.width, canvas.height) const imgUrl = canvas.toDataURL() // 在这里可以上传 imgUrl 到服务器或者进行其他操作 } } function handleError (e) { console.log(e) } ``` 这个例子中,我们首先使用 `desktopCapturer.getSources` 来获取可以捕获的源,然后选择一个源来获取媒体流,最后使用 `canvas` 来抓取视频帧并将其转换为图片数据。你可以使用这个图片数据来上传到服务器或者进行其他操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值