From 6c7bcf476f1286353a4869492598e2b537d7391f Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Thu, 3 Aug 2023 18:31:07 +0300 Subject: [PATCH] [TEST!] Added termux handle volume for try switch tracks --- src/android_termux_api.py | 33 +++++++++++++++++++++++++++++++++ src/mpv_control.py | 3 +++ src/settings.py | 3 ++- 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/android_termux_api.py diff --git a/src/android_termux_api.py b/src/android_termux_api.py new file mode 100644 index 0000000..9809da7 --- /dev/null +++ b/src/android_termux_api.py @@ -0,0 +1,33 @@ +import subprocess +import os +import json +import threading +from src.mpv_control import player +from loguru import logger +'''Warning! This module can be very battery drain''' + + +@logger.catch +def handle_vol_lvl_as_switch_track(): + volume_diff = [] + while True: + volume = subprocess.Popen("termux-volume", stdout=subprocess.PIPE).stdout + json_volume = json.loads(volume.read()) + for i in json_volume: + if i['stream'] == 'music': + volume_diff.append(i['volume']) + if len(volume_diff) == 2: + before, after = volume_diff + difference = after - before + if difference == 2: + try: + player.playlist_next() + except: + pass + os.system(f'termux-volume music {before}') + volume_diff = [] + + +handle_vol_lvl = threading.Thread( + target=handle_vol_lvl_as_switch_track, daemon=True) +handle_vol_lvl.start() diff --git a/src/mpv_control.py b/src/mpv_control.py index 6602ae8..c78b77b 100644 --- a/src/mpv_control.py +++ b/src/mpv_control.py @@ -27,6 +27,9 @@ share_to_fediverse_token = get_config('share_to_fediverse_token') share_to_fediverse_instance = get_config('share_to_fediverse_instance') shuffle = False +if get_config('termux_handle_track_switch_by_volume'): + import src.android_termux_api + class player_fw_storage: storage = {} diff --git a/src/settings.py b/src/settings.py index 1cc9c81..7fd9b2a 100644 --- a/src/settings.py +++ b/src/settings.py @@ -44,7 +44,8 @@ default_conf = { 'enable_persistent_cache': False, 'mpv_volume': 100, 'show_like_button': True, - 'show_artist_name_in_albums': False + 'show_artist_name_in_albums': False, + 'termux_handle_track_switch_by_volume': False }