mirror of
https://github.com/onyx-and-iris/obsws-python.git
synced 2024-11-22 12:50:53 +00:00
upd examples, they now use context managers
This commit is contained in:
parent
cac236c004
commit
2cebd5eedb
@ -17,6 +17,12 @@ class Observer:
|
||||
print(f"Registered events: {self._client.callback.get()}")
|
||||
self.running = True
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_value, exc_traceback):
|
||||
self._client.disconnect()
|
||||
|
||||
def on_current_program_scene_changed(self, data):
|
||||
"""The current program scene has changed."""
|
||||
print(f"Switched to scene {data.scene_name}")
|
||||
@ -31,13 +37,11 @@ class Observer:
|
||||
|
||||
def on_exit_started(self, _):
|
||||
"""OBS has begun the shutdown process."""
|
||||
print(f"OBS closing!")
|
||||
self._client.unsubscribe()
|
||||
print("OBS closing!")
|
||||
self.running = False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
observer = Observer()
|
||||
|
||||
while observer.running:
|
||||
time.sleep(0.1)
|
||||
with Observer() as observer:
|
||||
while observer.running:
|
||||
time.sleep(0.1)
|
||||
|
@ -1,6 +1,7 @@
|
||||
import inspect
|
||||
|
||||
import keyboard
|
||||
|
||||
import obsws_python as obs
|
||||
|
||||
|
||||
@ -10,6 +11,12 @@ class Observer:
|
||||
self._client.callback.register(self.on_current_program_scene_changed)
|
||||
print(f"Registered events: {self._client.callback.get()}")
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_value, exc_traceback):
|
||||
self._client.disconnect()
|
||||
|
||||
@property
|
||||
def event_identifier(self):
|
||||
return inspect.stack()[1].function
|
||||
@ -31,13 +38,12 @@ def set_scene(scene, *args):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
req_client = obs.ReqClient()
|
||||
observer = Observer()
|
||||
with obs.ReqClient() as req_client:
|
||||
with Observer() as observer:
|
||||
keyboard.add_hotkey("0", version)
|
||||
keyboard.add_hotkey("1", set_scene, args=("START",))
|
||||
keyboard.add_hotkey("2", set_scene, args=("BRB",))
|
||||
keyboard.add_hotkey("3", set_scene, args=("END",))
|
||||
|
||||
keyboard.add_hotkey("0", version)
|
||||
keyboard.add_hotkey("1", set_scene, args=("START",))
|
||||
keyboard.add_hotkey("2", set_scene, args=("BRB",))
|
||||
keyboard.add_hotkey("3", set_scene, args=("END",))
|
||||
|
||||
print("press ctrl+enter to quit")
|
||||
keyboard.wait("ctrl+enter")
|
||||
print("press ctrl+enter to quit")
|
||||
keyboard.wait("ctrl+enter")
|
||||
|
@ -9,6 +9,8 @@ LEVELTYPE = IntEnum(
|
||||
start=0,
|
||||
)
|
||||
|
||||
DEVICE = "Desktop Audio"
|
||||
|
||||
|
||||
def on_input_mute_state_changed(data):
|
||||
"""An input's mute state has changed."""
|
||||
@ -32,15 +34,14 @@ def on_input_volume_meters(data):
|
||||
|
||||
|
||||
def main():
|
||||
client = obs.EventClient(subs=(obs.Subs.LOW_VOLUME | obs.Subs.INPUTVOLUMEMETERS))
|
||||
client.callback.register([on_input_volume_meters, on_input_mute_state_changed])
|
||||
with obs.EventClient(
|
||||
subs=(obs.Subs.LOW_VOLUME | obs.Subs.INPUTVOLUMEMETERS)
|
||||
) as client:
|
||||
client.callback.register([on_input_volume_meters, on_input_mute_state_changed])
|
||||
|
||||
while cmd := input("<Enter> to exit>\n"):
|
||||
if not cmd:
|
||||
break
|
||||
while _ := input("<Enter> to exit>\n"):
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
DEVICE = "Desktop Audio"
|
||||
|
||||
main()
|
||||
|
Loading…
Reference in New Issue
Block a user