当前位置: 首页 > 后端技术 > Node.js

electron播放rtmp流实现

时间:2023-04-03 19:31:20 Node.js

在基于vuecli3和electron的框架中,实现rtmp流的播放。为什么要写这篇文章,因为它的实现确实有很多坑,而且不能像web那样导入videoJS完成,闲话少说,代码:第一个坑:需要用到的库vue-video-playervideojs-flash注意:以上两个库必须用npm安装。我试过了,用其他方式安装完成后,会出现这个错误:flashtechisundefined。如果遇到这个错误,一定要先npmuninstallvue-video-player,然后重新安装一遍。第二个坑里播放rtmp需要flash的支持。Electron的实现需要代码加载flash的dll文件来获得支持。Electron的app类中有一个getPath方法。执行以下代码获取本机flash文件的路径:app.getPath('pepperFlashSystemPlugin')我运行时得到的地址是:C:WINDOWSsystem32MacromedFlashpepflashplayer64_32_0_0_414.dll所以让electron支持flash代码:app.commandLine.appendSwitch('ppapi-flash-path',app.getPath('pepperFlashSystemPlugin'));当然,最好的解决方案是能够直接将dll文件打包到项目中并随项目一起走,通过在vue.config.js中添加如下代码来实现pluginOptions:{electronBuilder:{builderOptions:{...win:{target:'nsis',icon:'./public/app.ico',extraResources:[{"from":"resources/","to":"./",}]}}}复制dll文件到resources文件文件夹(根目录与vue.config.js同级),然后修改文件加载路径:letplugins=''if(process.arch=='x64'){plugins=process.env.WEBPACK_DEV_SERVER_URL?path.join(__dirname,`../resources/pepflashplayer64_32_0_0_238.dll`):path.join(__dirname,`../pepflashplayer64_32_0_0_238.dll`)}else{plugins=process.env.WEBPACK_DEV_SERVER_URL?path.join(__dirname,`../resources/pepflashplayer32_29_0_0_238.dll`):path.join(__dirname,`../pepflashplayer32_29_0_0`238}.dllapp.commandLine.appendSwitch('ppapi-flash-path',plugins)第三个以上的东西基本可以玩,但是打包后发现不行,好像是flash加载不出来。静态文件的地址,然后渲染进程通过这个服务器获取到页面,然后就可以启动flash了上代码:'usestrict'import{app,BrowserWindow,ipcMain}from'electron'import{createProtocol}from'vue-cli-plugin-electron-builder/lib'//importinstallExtension,{VUEJS_DEVTOOLS}from'electron-devtools-installer'constisDevelopment=process.env.NODE_ENV!=='production'//保留窗口对象的全局引用,否则,当JavaScript对象被垃圾回收时,窗口将//自动关闭。让winimport从'express'constnet=require('net');functionportInUse(port){returnnewPromise((resolve,reject)=>{让server=net.createServer().listen(port);服务器.on('listening',function(){server.close();resolve(port);});server.on('error',function(err){if(err.code=='EADDRINUSE'){port++;reject(err);}});});}consttryUsePort=function(port,_portAvailableCallback){portInUse(port).then((port)=>{_portAvailableCallback(端口);}).catch((err)=>{port++;tryUsePort(port,_portAvailableCallback);})}tryUsePort(9080,function(port){console.log(port+"====端口:"+port+"可用====\n");//解决flash本地文件不安全问题if(process.env.NODE_ENV==="production"){letserver=express();server.use(express.static(__dirname));server.listen(port);}constwinURL=`http://localhost:${port}/index.html`console.log(winURL)constpath=require('path')letplugins=''if(process.arch=='x64'){plugins=process.env.WEBPACK_DEV_SERVER_URL?path.join(__dirname,`../resources/pepflashplayer64_32_0_0_238.dll`):path.join(__dirname,`../pepflashplayer64_32_0_0_238.dll`)}else{//plugins=process.env.WEBPACK_DEV_SERVER_URL?path.join(__dirname,`../resources/pepflashplayer32_29_0_0_238.dll`):path.join(__dirname,`../pepflashplayer32_29_0_0_238.dll`)plugins=app.getPath('pepperFlashSystemPlugin')}app.commandLine.appendSwitch('ppapi-flash-path',plugins)functioncreateWindow(){//创建浏览器窗口。win=newBrowserWindow({width:1000,height:580,webPreferences:{nodeIntegration:true,plugins:true,webSecurity:false//取消跨域},autoHideMenuBar:true,maximizable:false,resizable:false,frame:false,})if(process.env.WEBPACK_DEV_SERVER_URL){win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)win.webContents.openDevTools()}else{createProtocol('app')win.loadURL(winURL)}win.webContents.openDevTools({mode:'detach'})win.on('closed',()=>{win=null})}//当所有窗口关闭时退出。app.on('window-all-closed',()=>{//在macOS上,应用程序及其菜单栏通常保持活动状态//直到用户使用Cmd+Q显式退出if(process.platform!=='darwin'){app.quit()}})app.on('activate',()=>{//在macOS上,当单击停靠栏图标并且没有其他窗口打开时,通常会在应用程序中重新创建一个窗口。if(win===null){createWindow()}})//当Electron完成初始化并准备好创建浏览器窗口时将调用此方法。//有些API只能在这个事件发生后才能使用。app.on('ready',async()=>{//if(isDevelopment&&!process.env.IS_TEST){////安装VueDevtools//try{//awaitinstallExtension(VUEJS_DEVTOOLS)//}catch(e){//console.error('VueDevtoolsfailedtoinstall:',e.toString())//}//}createWindow()})以上是部分代码,可以清楚的看到处理逻辑,第四个坑端口被占用,通过net对象写一个递归加一点判断获取当前可用的端口。代码就像上面的第五个坑。现在你觉得大功告成了,但也只能高兴一阵子。在某些情况下,仍然会出现闪存加载崩溃的情况。这时候,你需要检查一下。你的flash版本问题,我试过30以上,但是29还可以,我推荐这个版本:pepflashplayer64_29_0_0_140.dllpepflashplayer32_29_0_0_140.dll备注:我上面的代码只兼容64位电脑,如果需要兼容32-位机,自己加点判断能