【问题标题】:Angular2 set audio currentTime in componentAngular2在组件中设置音频currentTime
【发布时间】:2017-09-23 17:30:22
【问题描述】:

以下是创建 HTML 5 音频元素的 html 内容:

  <audio id ="audio2"controls="controls" autoplay="false" (canplay)="CanPlay($event)">
            <source src="http://localhost:51657/Audio/1" type="audio/mp3"> 
</audio>

下面是我尝试将当前时间设置为某个默认值的组件,注释代码工作正常,但是请建议如何以 Angular2 的方式做同样的事情?。

    import { OnInit, Output,Component, Injectable, EventEmitter } from '@angular/core';
    declare let $: any;
@Component({
    selector: 'audio-tag',
  templateUrl: './audio-tag.component.html',
})

@Injectable()
export class AudioComponent implements OnInit {
 @Output() canplay: EventEmitter<any> = new EventEmitter();
   audio:any;
    playBtn: any;
    pauseBtn: any
    currentTime:any;
    timer:any;
    duration:any;

    sound: any;
    ngOnInit(): void {
//     $('#audio2').bind('canplay', function() {
//   this.currentTime = 20;
// });
          }       

    CanPlay(){
      console.log('canplay');        
      this.currentTime=10;
      this.audio.currentTime = 20;
    }
}

【问题讨论】:

    标签: html angular html5-audio angular2-template


    【解决方案1】:

    AudioComponent 应该用@Component({...}) 装饰而不是@Injectable()

    @Component({
    selector: 'my-audio-player',
     templateUrl: `
        <audio id ="audio2"controls="controls" autoplay="false" 
      (canplay)="CanPlay($event)">
            <source src="http://localhost:51657/Audio/1" type="audio/mp3"> 
     </audio>
    `
    }) 
    export class AudioComponent implements OnInit { ... }
    

    【讨论】:

    • 更新了组件的代码,问题是如何用 angular one 替换注释的 jquery 部分
    猜你喜欢
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多