首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Angular12中显示视频错误

如何在Angular12中显示视频错误
EN

Stack Overflow用户
提问于 2021-10-06 08:26:23
回答 1查看 64关注 0票数 0

我的目标是在视频文件损坏时显示用户可读的错误。我发现这段代码非常适合我的用例,但是我似乎不能让它工作……我甚至尝试将它直接放在模板中的纯JS中,但它仍然不起作用。

我的html:

代码语言:javascript
复制
<nb-card>

<video src="tgif.vid"
  autoplay
  controls
  onerror="errorMessageVideo(event)">
</video>

</nb-card>

我的Ts:

代码语言:javascript
复制
import { Component, ViewChild, ElementRef, OnInit } from '@angular/core';

@Component({
  selector: 'ngx-video-card',
  templateUrl: './video-card.component.html',
  styleUrls: ['./video-card.component.scss']
})
export class VideoCardComponent implements OnInit  {

ngOnInit() {
  this.errorMessageVideo(Event);
}
  errorMessageVideo(e) {
    // video playback failed - show a message saying why
    switch (e.target.error.code) {
      case e.target.error.MEDIA_ERR_ABORTED:
        alert('You aborted the video playback.');
        break;
      case e.target.error.MEDIA_ERR_NETWORK:
        alert('A network error caused the video download to fail part-way.');
        break;
      case e.target.error.MEDIA_ERR_DECODE:
        alert('The video playback was aborted due to a corruption problem or because the video used features your browser did not support.');
        break;
      case e.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
        alert('The video could not be loaded, either because the server or network failed or because the format is not supported.');
        break;
      default:
        alert('An unknown error occurred.');
        break;
    }
  }
}

有没有人能帮帮我

EN

回答 1

Stack Overflow用户

发布于 2021-10-06 08:35:45

您的模板有两个问题。

您需要使用括号绑定事件,并在模板中更改错误事件的名称。此外,您还需要使用$event而不是event来传递事件,如下所示:

代码语言:javascript
复制
<video src="tgif.vid"
  autoplay
  controls
  (error)="errorMessageVideo($event)">
</video>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69462229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档