Skip to content

Commit e272b69

Browse files
committed
Fixing volume issue with mpv
1 parent bad7717 commit e272b69

File tree

2 files changed

+101
-13
lines changed

2 files changed

+101
-13
lines changed

Changelog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2019-12-22 s-n-g
2+
* Fixing volume issue with mpv
3+
14
2019-12-14 s-n-g
25
* Version 0.8.6
36
* Adding playlist history (for local playlists)

pyradio/player.py

Lines changed: 98 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
from os.path import expanduser
77
from sys import platform, version_info
88
from sys import exit
9+
from time import sleep
10+
import json
11+
import socket
912

1013
logger = logging.getLogger(__name__)
1114

@@ -216,18 +219,20 @@ def updateStatus(self, *args):
216219
logger.debug("User input: {}".format(subsystemOut))
217220
self.oldUserInput['Input'] = subsystemOut
218221
if self.volume_string in subsystemOut:
219-
#logger.error("***** volume")
220-
if self.oldUserInput['Volume'] != subsystemOut:
221-
self.oldUserInput['Volume'] = subsystemOut
222-
self.volume = ''.join(c for c in subsystemOut if c.isdigit())
223-
224-
# IMPORTANT: do this here, so that cvlc actual_volume
225-
# gets updated in _format_volume_string
226-
string_to_show = self._format_volume_string(subsystemOut) + self._format_title_string(self.oldUserInput['Title'])
227-
228-
if self.show_volume and self.oldUserInput['Title']:
229-
self.outputStream.write(string_to_show, args[0])
230-
self.threadUpdateTitle(args[0])
222+
# disable volume for mpv
223+
if self.PLAYER_CMD != "mpv":
224+
logger.error("***** volume")
225+
if self.oldUserInput['Volume'] != subsystemOut:
226+
self.oldUserInput['Volume'] = subsystemOut
227+
self.volume = ''.join(c for c in subsystemOut if c.isdigit())
228+
229+
# IMPORTANT: do this here, so that cvlc actual_volume
230+
# gets updated in _format_volume_string
231+
string_to_show = self._format_volume_string(subsystemOut) + self._format_title_string(self.oldUserInput['Title'])
232+
233+
if self.show_volume and self.oldUserInput['Title']:
234+
self.outputStream.write(string_to_show, args[0])
235+
self.threadUpdateTitle(args[0])
231236
elif self._is_in_playback_token(subsystemOut):
232237
if self.connection_timeout_thread is not None:
233238
self.connection_timeout_thread.cancel()
@@ -576,19 +581,99 @@ def _stop(self):
576581
def _volume_up(self):
577582
""" increase mpv's volume """
578583
os.system("echo 'cycle volume' | socat - " + self.mpvsocket + " 2>/dev/null");
584+
self._diaplay_mpv_volume_value()
579585

580586
def _volume_down(self):
581587
""" decrease mpv's volume """
582588
os.system("echo 'cycle volume down' | socat - " + self.mpvsocket + " 2>/dev/null");
589+
self._diaplay_mpv_volume_value()
583590

584591
def _format_title_string(self, title_string):
585592
""" format mpv's title """
586593
return self._title_string_format_text_tag(title_string.replace(self.icy_tokkens[0], self.icy_title_prefix))
587594

588595
def _format_volume_string(self, volume_string):
589-
""" format mplayer's volume """
596+
""" format mpv's volume """
590597
return '[' + volume_string[volume_string.find(self.volume_string):].replace('ume', '')+'] '
591598

599+
def _connect_to_socket(self, server_address):
600+
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
601+
try:
602+
sock.connect(server_address)
603+
return sock
604+
except socket.error as err:
605+
sock.close()
606+
return None
607+
608+
def _diaplay_mpv_volume_value(self):
609+
""" Display volume for MPV
610+
611+
Currently working with python 2 and 3
612+
Eventually will be used for python 2 only
613+
614+
Python 2 cannot correctly read icy-title from
615+
the socket (unidoce issue), so it has to read
616+
it from stdout.
617+
"""
618+
619+
#if version_info > (3, 0):
620+
# return
621+
vol = 0
622+
while True:
623+
sock = self._connect_to_socket(self.mpvsocket)
624+
if sock:
625+
break
626+
sleep(.25)
627+
628+
# Send data
629+
message = b'{ "command": ["get_property", "volume"] }\n'
630+
sock.sendall(message)
631+
632+
# wait for response
633+
got_it = True
634+
while got_it:
635+
try:
636+
if version_info < (3, 0):
637+
data = sock.recv(4096)
638+
else:
639+
data = sock.recvmsg(4096)
640+
if isinstance(data, tuple):
641+
a_data = data[0]
642+
else:
643+
a_data = data
644+
#logger.error('DE Received: "{!r}"'.format(a_data))
645+
646+
if a_data == b'':
647+
break
648+
649+
if data:
650+
651+
all_data = a_data.split(b'\n')
652+
for n in all_data:
653+
try:
654+
d = json.loads(n)
655+
if d['error'] == 'success':
656+
try:
657+
vol = int(d['data'])
658+
got_it = False
659+
break
660+
except:
661+
pass
662+
except:
663+
pass
664+
finally:
665+
pass
666+
sock.close()
667+
if self.oldUserInput['Title']:
668+
info_string = self._format_title_string(self.oldUserInput['Title'])
669+
else:
670+
info_string = self._format_title_string(self.oldUserInput['Input'])
671+
string_to_show = self._format_volume_string('Volume: ' + str(vol) + '%') + info_string
672+
self.outputStream.write(string_to_show)
673+
self.threadUpdateTitle(self.status_update_lock)
674+
self.volume = str(vol)
675+
676+
592677
class MpPlayer(Player):
593678
"""Implementation of Player object for MPlayer"""
594679

0 commit comments

Comments
 (0)