Skip to content

Commit

Permalink
add debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Krozark committed Feb 8, 2022
1 parent ad592b3 commit 099e889
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sound_player/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.3.2"
__version__ = "0.3.3"


import logging
Expand Down
11 changes: 10 additions & 1 deletion sound_player/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, callback, **kwargs):

@java_method("(Landroid/media/MediaPlayer;)V")
def onCompletion(self, mp):
logger.debug("OnCompletionListener.onCompletion()")
self.callback()


Expand All @@ -37,38 +38,45 @@ def __del__(self):
self._unload()

def _do_play(self):
logger.debug("AndroidSound._do_play()")
if not self._mediaplayer:
self._load()
self._mediaplayer.start()
# elif self._status == STATUS.PAUSED:
# self._mediaplayer.start()

def _do_pause(self):
self._mediaplayer.pause()
logger.debug("AndroidSound._do_pause()")
self._mediaplayer.pause()

def _do_stop(self):
logger.debug("AndroidSound._do_stop()")
# if not self._mediaplayer:
# return
self._mediaplayer.stop()
self._mediaplayer.prepare()

def _completion_callback(self):
logger.debug("AndroidSound._completion_callback()")
#super().stop()
self._status = STATUS.STOPPED

def _load(self):
logger.debug("AndroidSound._load()")
self._unload()
self._completion_listener = OnCompletionListener(
self._completion_callback
)

self._mediaplayer = MediaPlayer()
if api_version >= 21:
logger.debug("API version >= 21")
self._mediaplayer.setAudioAttributes(
AudioAttributesBuilder()
.setLegacyStreamType(AudioManager.STREAM_MUSIC)
.build())
else:
logger.debug("API version < 21")
self._mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC)

self._mediaplayer.setDataSource(self._filepath)
Expand All @@ -77,6 +85,7 @@ def _load(self):
self._mediaplayer.prepare()

def _unload(self):
logger.debug("AndroidSound._unload()")
if self._mediaplayer:
self._mediaplayer.release()
self._mediaplayer = None
Expand Down
4 changes: 4 additions & 0 deletions sound_player/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ def __init__(self):
self._status = STATUS.STOPPED

def status(self):
logger.debug("StatusObject,status()")
return self._status

def play(self):
logger.debug("StatusObject,play()")
self._status = STATUS.PLAYING

def pause(self):
logger.debug("StatusObject,pause()")
self._status = STATUS.PAUSED

def stop(self):
logger.debug("StatusObject,stop()")
self._status = STATUS.STOPPED
7 changes: 7 additions & 0 deletions sound_player/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __del__(self):
self._popen = None

def _build_options(self):
logger.debug("FFMpegSound,_build_options()")
player = None
if self.which("avplay"):
player = "avplay"
Expand All @@ -38,14 +39,17 @@ def _build_options(self):
return options

def _create_popen(self):
logger.debug("FFMpegSound,_create_popen()")
args = self._build_options()
self._popen = subprocess.Popen(args)

def wait(self, timeout=None):
logger.debug("FFMpegSound,wait(%s)", timeout)
code = self._popen.wait(timeout=timeout)
return code

def poll(self):
logger.debug("FFMpegSound,poll()")
if self._popen:
code = self._popen.poll()
if code is not None:
Expand All @@ -58,15 +62,18 @@ def poll(self):
return self._status

def _do_play(self):
logger.debug("FFMpegSound,_do_play()")
if self._popen is None:
self._create_popen()
elif self._status == STATUS.PAUSED:
self._popen.send_signal(signal.SIGCONT)

def _do_pause(self):
logger.debug("FFMpegSound,_do_pause()")
self._popen.send_signal(signal.SIGSTOP)

def _do_stop(self):
logger.debug("FFMpegSound,_do_stop()")
if self._popen:
self._popen.kill()
self._popen = None
15 changes: 15 additions & 0 deletions sound_player/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ def __init__(self, concurency=1, replace=False, loop=1):
self._lock = threading.Lock()

def set_concurency(self, concurency):
logger.debug("Playlist.set_concurency(%s)", concurency)
self._concurency = concurency

def set_replace(self, replace):
logger.debug("Playlist.set_replace(%s)", replace)
self._replace_on_add = replace

def set_loop(self, loop):
logger.debug("Playlist.set_loop(%s)", loop)
self._loop = loop

def enqueue(self, sound):
logger.debug("Playlist.enqueue(%s)", sound)
with self._lock:
logger.debug("enqueue %s" % sound)
loop = sound._loop or self._loop
Expand All @@ -37,17 +41,20 @@ def enqueue(self, sound):
self._queue_waiting.append(sound)

def clear(self):
logger.debug("Playlist.clear()")
with self._lock:
self._queue_waiting.clear()
self._queue_current.clear()

def pause(self):
logger.debug("Playlist.pause()")
super().pause()
with self._lock:
for sound in self._queue_current:
sound.pause()

def stop(self):
logger.debug("Playlist.stop()")
super().stop()
with self._lock:
for sound in self._queue_current:
Expand All @@ -56,6 +63,7 @@ def stop(self):
self.clear()

def play(self):
logger.debug("Playlist.play()")
super().play()
if self._thread is None:
logger.debug("Create playlist Thread")
Expand Down Expand Up @@ -106,6 +114,7 @@ def __init__(self):
self._playlists = defaultdict(Playlist)

def enqueue(self, sound, playlist):
logger.debug("SoundPlayer.enqueue(%s, %s)", sound, playlist)
if not playlist in self._playlists:
if self._status == STATUS.PLAYING:
self._playlists[playlist].play()
Expand All @@ -114,18 +123,22 @@ def enqueue(self, sound, playlist):
self._playlists[playlist].enqueue(sound)

def status(self, playlist=None):
logger.debug("SoundPlayer.status(%s)", playlist)
if playlist is not None:
return self._playlists[playlist].status()
return super().status()

def get_playlists(self):
logger.debug("SoundPlayer.get_playlists()")
return self._playlists.keys()

def delete_playlist(self, playlist):
logger.debug("SoundPlayer.delete_playlist(%s)", playlist)
self._playlists[playlist].stop()
del self._playlists[playlist]

def play(self, playlist=None):
logger.debug("SoundPlayer.play(%s)", playlist)
if playlist is not None:
return self._playlists[playlist].play()
else:
Expand All @@ -135,6 +148,7 @@ def play(self, playlist=None):
super().play()

def pause(self, playlist=None):
logger.debug("SoundPlayer.pause(%s)", playlist)
if playlist is not None:
return self._playlists[playlist].pause()
else:
Expand All @@ -144,6 +158,7 @@ def pause(self, playlist=None):
super().pause()

def stop(self, playlist=None):
logger.debug("SoundPlayer.stop(%s)", playlist)
if playlist is not None:
return self._playlists[playlist].stop()
else:
Expand Down
6 changes: 6 additions & 0 deletions sound_player/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ def __init__(self, filepath, loop=None):
self._loop = loop

def set_loop(self, loop):
logger.debug("BaseSound.set_loop(%s)", loop)
self._loop = loop

def play(self):
logger.debug("BaseSound.play()")
if self.status() == STATUS.PLAYING:
return
elif self._status not in (STATUS.STOPPED, STATUS.PAUSED):
Expand All @@ -27,6 +29,7 @@ def play(self):
super().play()

def pause(self):
logger.debug("BaseSound.pause()")
if self.status() == STATUS.PAUSED:
return
elif self._status != STATUS.PLAYING:
Expand All @@ -36,6 +39,7 @@ def pause(self):
super().pause()

def stop(self):
logger.debug("BaseSound.stop()")
if self.status() == STATUS.STOPPED:
return
elif self._status not in (STATUS.PLAYING, STATUS.PAUSED):
Expand All @@ -48,12 +52,14 @@ def wait(self, timeout=None):
raise NotImplementedError

def poll(self):
logger.debug("BaseSound.poll()")
return self._status

def which(self, program):
"""
Mimics behavior of UNIX which command.
"""
logger.debug("BaseSound.wich(%s)", program)
# Add .exe program extension for windows support
if platform == "windows" and not program.endswith(".exe"):
program += ".exe"
Expand Down

0 comments on commit 099e889

Please sign in to comment.