使用MUX Player(视频)在Angular 14/15中。

huangapple go评论83阅读模式
英文:

Use MUX Player (Video) in Angular 14/15

问题

Mux 是一个视频 API 服务,它有自己的播放器:MUX Player

我想在 Angular 14/15 中只在特定组件中导入这个 npm 包。当加载这个组件时,应该只加载这个 js。

导入

使用这个服务的两种方法是:

  1. 通过在 HTML 中直接导入 <script src="https://unpkg.com/@mux/mux-player"></script>(这可以在 Angular 的 index.html 文件中完成)。但是这意味着脚本会加载给每个人,这不是我想要的。
  2. 使用 npm 包 @mux/mux-player

使用

将代码添加到已经导入脚本或 npm 包的组件的 HTML 中。

<mux-player
stream-type="on-demand"
playback-id="FuJSYrK0014ec2LPnm11bzC2MAySAQPqA"
metadata-video-title="Tea and Cookies"
metadata-user-id="user-24601"
/>

确保在模块(或独立组件)中也包含 schemas: [CUSTOM_ELEMENTS_SCHEMA]

问题

Angular 在处理无类型的包时可能有点奇怪。我认为这个包使用了 TypeScript,但无论如何,导入 npm 包时我遇到了很多错误。基本上,它没有被加载,因为 Angular 在组件的 ts 文件中没有使用导入的类时会将它 tree-shake 掉。这意味着在加载时,我看到了以下错误:
Cannot find module '../../../types/mux' or its corresponding type declarationsProperty 'error' in type 'MuxVideoElement' is not assignable to the same property in base type 'CustomVideoElement<HTMLVideoElement>'. Type 'MediaError' is missing the following properties from type 'MediaError': MEDIA_ERR_ABORTED, MEDIA_ERR_DECODE, MEDIA_ERR_NETWORK, MEDIA_ERR_SRC_NOT_SUPPORTED

我尝试过的事情

  1. 为了确认 <script> 导入和 HTML 代码可以配合使用,我将脚本导入到了 index.html 中,这样就移除了所有错误,播放器正常加载了。

  2. 我尝试导入 npm 包 import MuxPlayerElement from "@mux/mux-player";,并在 typings.d.ts 中添加了 declare module "@mux/mux-player";

  3. 将包导入到组件中,并尝试将 @ViewChild 模板引用声明为 MuxPlayerElement,以绕过任何 tree-shaking。

在 Angular 的演示中,有一个地方说:“这个播放器?它是一本敞开的书。”,网址是 https://www.mux.com/player,然而,这会导致错误 Only void and foreign elements can be self closed "mux-player"。简单的解决方法是用 </mux-player> 而不是 .../> 来关闭标签。

英文:

Mux is a video API service that has its own player: MUX Player

I am wanting to use this npm package in Angular 14/15 by importing it into a specific component only. The js should only be loaded when this component is loaded.

Import

The two methods of using the service are:

  1. via a direct <script src="https://unpkg.com/@mux/mux-player"></script> import in the HTML (which I can do in angular's index.html file. This does however mean that the script is loaded for everyone, which is not what I want.
  2. npm Package @mux/mux-player

Usage

Add the code to a component's HTML where the script or npm package has been imported.

<mux-player
stream-type="on-demand"
playback-id="FuJSYrK0014ec2LPnm11bzC2MAySAQPqA"
metadata-video-title="Tea and Cookies"
metadata-user-id="user-24601"
/>

Making sure to also include schemas: [CUSTOM_ELEMENTS_SCHEMA] in the module (or standalone component).

Problem

Angular can be a bit weird about untyped packages. I think that this package uses typescript, but in any case, importing the npm package gives me plenty of errors. Essentially, it's not being loaded because Angular is tree-shaking it out when I am not using the imported class in the components' ts. This means that on load, I am seeing:
Cannot find module '../../../types/mux' or its corresponding type declarations and Property 'error' in type 'MuxVideoElement' is not assignable to the same property in base type 'CustomVideoElement<HTMLVideoElement>'.
Type 'MediaError' is missing the following properties from type 'MediaError': MEDIA_ERR_ABORTED, MEDIA_ERR_DECODE, MEDIA_ERR_NETWORK, MEDIA_ERR_SRC_NOT_SUPPORTED

What I've tried

  1. To confirm that the <script> import and HTML code work together, I imported the script into the index.html, and this removed all errors and the player loaded fine.

  2. I have tried importing the npm package import MuxPlayerElement from "@mux/mux-player";, and added declare module "@mux/mux-player"; to the typings.d.ts.

  3. Import the package into the component, and tried to declare the @ViewChild template ref as a MuxPlayerElement, to get around any tree-shaking.

There is an Angular demo where it says "This player? It's an open book." at https://www.mux.com/player, however, this results in the error Only void and foreign elements can be self closed "mux-player". Easy solution to this is to close the tag with </mux-player> rather than .../>

答案1

得分: 1

更新!

Mux 在 1.9.0 版本中解决了对 Angular 的支持

https://docs.mux.com/guides/player/releases#190

使用说明:

  1. npm install @mux/mux-player
  2. schemas: [CUSTOM_ELEMENTS_SCHEMA] 添加到模块(或独立组件)
  3. import "@mux/mux-player" 添加到目标组件的导入部分
  4. 如文档中所述使用 <mux-player ...></mux-player>
英文:

Update!

Mux addressed angular support in 1.9.0

https://docs.mux.com/guides/player/releases#190

Instructions for usage:

  1. npm install @mux/mux-player
  2. add schemas: [CUSTOM_ELEMENTS_SCHEMA] to module (or standalone component)
  3. add import "@mux/mux-player" to the target component imports
  4. Use as documented <mux-player ...></mux-player>

🎉

huangapple
  • 本文由 发表于 2023年2月14日 04:22:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定