Skip to content

Commit 3e18edf

Browse files
committed
add played_seconds function
1 parent ebde918 commit 3e18edf

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

discord/player.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ def __init__(self, source: AudioSource, client: VoiceClient, *, after=None):
724724
self._current_error: Exception | None = None
725725
self._connected: threading.Event = client._connected
726726
self._lock: threading.Lock = threading.Lock()
727+
self._played_frames: int = 0
727728

728729
if after is not None and not callable(after):
729730
raise TypeError('Expected a callable for the "after" parameter.')
@@ -755,6 +756,8 @@ def _do_run(self) -> None:
755756
self._start = time.perf_counter()
756757

757758
self.loops += 1
759+
self._played_frames += 1
760+
758761
# Send the data read from the start of the function if it is not None
759762
if first_data is not None:
760763
data = first_data
@@ -834,3 +837,6 @@ def _speak(self, speaking: bool) -> None:
834837
)
835838
except Exception as e:
836839
_log.info("Speaking call in player failed: %s", e)
840+
841+
def played_seconds(self) -> int:
842+
return self._played_frames // 50

discord/voice_client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,3 +988,10 @@ def send_audio_packet(self, data: bytes, *, encode: bool = True) -> None:
988988
)
989989

990990
self.checked_add("timestamp", opus.Encoder.SAMPLES_PER_FRAME, 4294967295)
991+
992+
def played_seconds(self) -> int:
993+
"""Gets the elapsed time of the playing audio in seconds. Returns 0 if not playing anything."""
994+
if self._player:
995+
return self._player.played_seconds()
996+
else:
997+
return 0

0 commit comments

Comments
 (0)