Angular 项目引入纯 H5 直播流播放器 Jessibuca(已被官方收录)

Angular 项目引入纯 H5 直播流播放器 Jessibuca(已被官方收录)

前言

最近工作 Angular 项目在做摄像监控的相关功能,需要使用 Jessibuca 插件,查阅官方文档发现只有 Vue 和 React 的 demo,百度 Google 也基本查不到有用的资料,啧啧啧- -,这还能忍!把我大 Angular 置于何地!遂自己写了个 Angular demo,贡献给官方: http://jessibuca.monibuca.com/demo.html#angular(由网友echeverra提供,非官方)。

本文介绍如何在 Angular 项目中引入纯 H5 直播流播放器 Jessibuca,可以在 Github: https://github.com/echeverra/Jessibuca-angular-demo 上下载,本地安装依赖运行,支持基础的播放、暂停和销毁功能。

简介

Angular 作为前端的三座大山,哦不- -,三大框架自当不必多说,由 Google 开发维护,是一个完善的前端框架,包含服务,模板,数据双向绑定,模块化,路由,过滤器,依赖注入等所有功能,不会像另外两个框架一样需要单独引入功能模块,缺点就是学习曲线陡峭,难度较大,是的,我还在硬啃中,边学边输出《玩转 Angular 系列》文章。

Jessibuca 是一款开源的纯H5直播流播放器,通过 Emscripten 将音视频解码库编译成 Js(wasm) 运行于浏览器之中。兼容几乎所有浏览器,可以运行在 PC、手机、微信中,无需额外安装插件。在 Github: https://github.com/langhuihui/jessibuca/tree/v3 上有 1.5k star,同时有比较完善的官方文档: http://jessibuca.monibuca.com/。

引入

咳咳,接下来重点来了,上面的你可以当做是凑数的废话了- -。

首先下载最新的 Jessibuca: https://jessibuca.com/dist.zip 文件,其中包含 jessibuca.js、jessibuca.d.ts、decoder.js、decoder.wasm 4个重要文件。

我们在 Angular 项目中(Angular CLI创建)assets 目录下创建目录 jessibuca,并将以上 4 个文件导入。

在入口文件 index.html 标签中引入 jessibuca.js。

在项目环境配置文件 angular.json 中配置好 scripts。

"scripts": [

"src/assets/jessibuca/jessibuca.js"

]

在将 jessibuca.js 文件中修改 decoder.js 路径。

decoder:"assets/jessibuca/decoder.js"

原本是不想修改源码的,但尝试在项目中将 decoder.js 和 decoder.wasm 放在根目录 src 目录下,一直会报 GET http://localhost:4200/decoder.js 404 (Not Found) 的错误,无奈出此下策。

最后在 ts 文件中要引入类 Jessibuca。

import Jessibuca from "src/assets/jessibuca/jessibuca";

最后附上 app.component.html 代码:

Angular jessibuca demo player

输入URL:

以及 app.component.ts 代码:

import { Component, OnInit, AfterViewInit, ViewChild, ElementRef } from '@angular/core';

import Jessibuca from "src/assets/jessibuca/jessibuca";

@Component({

selector: 'app-root',

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']

})

export class AppComponent implements OnInit, AfterViewInit {

title = 'jess';

@ViewChild('container') playContainer!: ElementRef;

playUrl: string = 'http://flv.bdplay.nodemedia.cn/live/bbb.flv';

playing: boolean = false;

jessibuca: any;

constructor() {

}

ngOnInit(): void {

}

ngAfterViewInit(): void {

this.jessibuca = new Jessibuca({

container: this.playContainer.nativeElement,

videoBuffer: 0.2, // 缓存时长

isResize: false,

loadingText: '加载中',

debug: true,

showBandwidth: true, // 显示网速

operateBtns: {

fullscreen: true,

screenshot: true,

play: true,

audio: true,

},

forceNoOffscreen: true,

isNotMute: false,

});

}

play(): void {

this.jessibuca.play(this.playUrl);

this.playing = true;

}

pause(): void {

this.jessibuca.pause(this.playUrl);

this.playing = false;

}

destroy(): void {

this.jessibuca.destroy();

this.playing = false;

this.playUrl = '';

}

}

完整项目代码大家可以在 Github:https://github.com/echeverra/Jessibuca-angular-demo 上查看,可以下载到本地运行。

在本地运行只需要执行两个命令,安装项目依赖 npm install,启动项目服务 ng serve 即可。

结语

虽然国内使用 Angular 框架的人不多,在 Angular 中使用 Jessibuca 的就更少了,不过还是希望能帮助到有需要的你~

好啦,以上就是 Angular 项目引入纯 H5 直播流播放器 Jessibuca 的全部内容,希望对你有所帮助。

你学“废”了么?

(完)

相关推荐

大鱼号运营如何在不同平台上进行多渠道传播? 365bet官网网投

大鱼号运营如何在不同平台上进行多渠道传播?

让你白起来!薇奴补水保湿清洁祛痘收缩毛孔蚕丝黑面膜 10片装 神器365软件下载

让你白起来!薇奴补水保湿清洁祛痘收缩毛孔蚕丝黑面膜 10片装

小型餐饮店如何提高营业额?新营销模式方法拆解 365bet官网网投

小型餐饮店如何提高营业额?新营销模式方法拆解