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', Lang.bind(this, this.refresh));
this.mixer_control.connect('stream-removed', Lang.bind(this, 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
,它没有警告或错误。
我找到了关于如何在 gnome shell 扩展中处理键盘事件的 StackOverflow 链接?,但是,我不希望这与特定的键盘事件相关联。相反,即使通过其他方式更改了麦克风级别,标签也应该得到更新。
我想我需要将它连接到一个信号或使用类似的东西,但是,我不知道该怎么做。我是 gnome 扩展的新手,所以详细的解释可能会有所帮助。
解决方案
您可能希望连接到该属性的notify
信号:Gvc.MixerStream
volume
stream.connect('notify::volume', (stream) => {
log(`New volume is ${stream.volume}`);
});
如果需要,您可以将其放入您的包装类中,也许将其设为GObject 子类。