Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
角度:我必须通过javascript禁用浏览器选项卡。所有组件中都没有音频对象,所以我需要使用公共脚本来暂停应用程序音频_Javascript_Jquery_Angular_Dom Events - Fatal编程技术网

角度:我必须通过javascript禁用浏览器选项卡。所有组件中都没有音频对象,所以我需要使用公共脚本来暂停应用程序音频

角度:我必须通过javascript禁用浏览器选项卡。所有组件中都没有音频对象,所以我需要使用公共脚本来暂停应用程序音频,javascript,jquery,angular,dom-events,Javascript,Jquery,Angular,Dom Events,角度:我必须通过javascript禁用浏览器选项卡。所有组件中都没有音频对象,所以我需要公共脚本来暂停应用程序音频 具有播放和暂停操作的每个组件中的音频对象数。但现在我想添加一个按钮,通过它我可以使浏览器选项卡静音。如果我愿意,我也可以启用它 我的当前组件对象(例如代码段): 如上所述,所有组件中都没有此类对象。我必须将toogle按钮添加到开/关应用程序声音中。为此,我需要禁用/取消禁用该按钮上的浏览器选项卡 请向我推荐脚本(javascript/jquery)或想法。创建一个可以创建任何媒

角度:我必须通过javascript禁用浏览器选项卡。所有组件中都没有音频对象,所以我需要公共脚本来暂停应用程序音频

具有播放和暂停操作的每个组件中的音频对象数。但现在我想添加一个按钮,通过它我可以使浏览器选项卡静音。如果我愿意,我也可以启用它

我的当前组件对象(例如代码段):

如上所述,所有组件中都没有此类对象。我必须将toogle按钮添加到开/关应用程序声音中。为此,我需要禁用/取消禁用该按钮上的浏览器选项卡


请向我推荐脚本(javascript/jquery)或想法。

创建一个可以创建任何媒体元素并跟踪它的服务。
使用该服务创建媒体元素的实例。
调用服务功能,使服务创建的所有媒体静音

正如您所说,您已经创建了多个音频/视频标签。您只需要重构一点就可以了。
这里要注意的主要问题是“不要直接创建新音频()”您无法跟踪您创建的内容。只需将创建逻辑移动到服务并调用服务方法

// Create a service class to handle media creations
class MediaService {
  
  media = []; // will hold all media references when created
  
  createNewAudio() {
    // create a Audio instance as usual.
    const audio = new Audio();
    // push the reference of the instance to media array 
    // to perform any operation in future.
    this.media.push(audio);
    
    // return the Audio instance created above.
    return audio;
  }
  
  
  createNewVideo() {
    // create a Video instance as usual.
    const video = new Video();
    // push the reference of the instance to media array 
    // to perform any operation in future.
    this.media.push(video);
    
    // return the Video instance created above.
    return video;
  }
  

  muteAllMedia() {
    this.media.map(m => m.pause());
  }
  
}


// Inject and Use the service to create any media elements
class MyComponent {
    
  constructor(private mediaService: MediaService) { }
  
  ngOnInit() {
    //in onInit function
    this.audioLogin = this.mediaService.createNewAudio();

    //in login page loading function
    this.audioLogin.play();
  }
  
  mute() {
    this.mediaService.muteAllMedia();
  }
}

我在我的应用程序中尝试了上述服务代码。。但它只是暂停当前播放的音频。您必须将使用new Audio()的所有位置更改为服务实现。只有这样服务才能知道所有媒体createdservice逻辑是否正确,但pause()方法仅适用于当前播放的音频“仅”,这是该服务的缺点。是的,我在所有地方都换了。你们还有什么音频?它是从哪里产生的?
// Create a service class to handle media creations
class MediaService {
  
  media = []; // will hold all media references when created
  
  createNewAudio() {
    // create a Audio instance as usual.
    const audio = new Audio();
    // push the reference of the instance to media array 
    // to perform any operation in future.
    this.media.push(audio);
    
    // return the Audio instance created above.
    return audio;
  }
  
  
  createNewVideo() {
    // create a Video instance as usual.
    const video = new Video();
    // push the reference of the instance to media array 
    // to perform any operation in future.
    this.media.push(video);
    
    // return the Video instance created above.
    return video;
  }
  

  muteAllMedia() {
    this.media.map(m => m.pause());
  }
  
}


// Inject and Use the service to create any media elements
class MyComponent {
    
  constructor(private mediaService: MediaService) { }
  
  ngOnInit() {
    //in onInit function
    this.audioLogin = this.mediaService.createNewAudio();

    //in login page loading function
    this.audioLogin.play();
  }
  
  mute() {
    this.mediaService.muteAllMedia();
  }
}