微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 gnome 扩展中处理麦克风声级更新事件?

如何解决如何在 gnome 扩展中处理麦克风声级更新事件?

我有一些全局快捷方式来更新麦克风声音输入电平。因此,我创建了一个 gnome 扩展,在顶部栏中添加一个标签显示当前麦克风声音百分比。

extension.js 中的代码是这样的:

const Microphone = new Lang.Class({
  Name: 'Microphone',_init: function() {
    this.active = null;
    this.stream = null;
    this.muted_changed_id = 0;
    this.mixer_control = new Gvc.mixerControl({name: 'Some random name'});
    this.mixer_control.open();
    this.mixer_control.connect('default-source-changed',Lang.bind(this,this.refresh));
    this.mixer_control.connect('stream-added',this.refresh));
    this.mixer_control.connect('stream-removed',this.refresh));
    this.stream = this.mixer_control.get_default_source();
  },// ...

  get level() {
    return 100 * this.stream.get_volume() / this.mixer_control.get_vol_max_norm();
  }
});

function enable() {
  // ...
  microphone = new Microphone();
  let panel_button_label = new St.Label({
    y_expand: true,y_align: clutter.ActorAlign.CENTER
  });
  panel_button_label.text = microphone.level + '%';
  Main.panel._rightBox.insert_child_at_index(panel_button_label,0);
}

function disable() {
  // ...
  Main.panel._rightBox.remove_child(panel_button_label);
  panel_button_label.destroy();
  panel_button_label = null;
}

但是,每次全局快捷方式更新麦克风级别时,我不知道如何更新 microphone.label 标签文本。截至目前,它始终显示为 0%。我查看了 journalctl 中的日志,没有警告或错误

我在 How to handle keyboard events in gnome shell extensions? 上找到了 StackOverflow 链接,但是,我不希望将其链接到特定的键盘事件。相反,即使通过其他方式更改了麦克风级别,标签也应该更新。

我想我需要将它连接到信号或使用类似的东西,但是,我不知道该怎么做。我是 gnome 扩展的新手,所以详细的解释可能会有所帮助。

解决方法

您可能希望为 notify 属性连接到 Gvc.MixerStreamvolume 信号:

stream.connect('notify::volume',(stream) => {
  log(`New volume is ${stream.volume}`);
});

如果需要,您可以将其添加到您的包装类中,或许可以将其设为 GObject subclass

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。