Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

How to handle microphone sound level update event in gnome extensions?

I have certain global shortcuts to update the microphone sound input level. Therefore, I was creating a gnome extension that adds a label in the top bar, displaying the current microphone sound percentage.

The code in extension.js is something like this:

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;
}

However, I don't know how to update the microphone.label label text each time the microphone level is updated by the global shortcut. As of now, it displays 0% always. I checked out the logs in journalctl, it has no warnings or errors.

I figured out a StackOverflow link on How to handle keyboard events in gnome shell extensions?, however, I don't want this to be linked to a particular keyboard event. Rather, the label should get updated even if the microphone level is changed through some other means.

I guess I need to connect this to a signal or use something similar to that, however, I don't know how to. I am new to gnome extensions, so a detailed explanation might be helpful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You probably want to connect to the notify signal of Gvc.MixerStream for the volume property:

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

You can work that into your wrapper class if you want, perhaps making it a GObject subclass.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...