Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ These changes are available on the `master` branch, but have not yet been releas
`tags`. ([#2520](https://github.com/Pycord-Development/pycord/pull/2520))
- Added `Member.guild_banner` and `Member.display_banner` properties.
([#2556](https://github.com/Pycord-Development/pycord/pull/2556))
- Added `played_time` function to `VoiceClient` to get elapsed time of the playing audio
file. ([#2587](https://github.com/Pycord-Development/pycord/pull/2587/))

### Fixed

Expand Down
7 changes: 7 additions & 0 deletions discord/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ def __init__(self, source: AudioSource, client: VoiceClient, *, after=None):
self._current_error: Exception | None = None
self._connected: threading.Event = client._connected
self._lock: threading.Lock = threading.Lock()
self._played_frames: int = 0

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

self.loops += 1
self._played_frames += 1

# Send the data read from the start of the function if it is not None
if first_data is not None:
data = first_data
Expand Down Expand Up @@ -834,3 +837,7 @@ def _speak(self, speaking: bool) -> None:
)
except Exception as e:
_log.info("Speaking call in player failed: %s", e)

def played_frames(self) -> int:
"""Gets the number of 20ms frames played since the start of the audio file."""
return self._played_frames
8 changes: 8 additions & 0 deletions discord/voice_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from __future__ import annotations

import asyncio
import datetime
import logging
import select
import socket
Expand Down Expand Up @@ -988,3 +989,10 @@ def send_audio_packet(self, data: bytes, *, encode: bool = True) -> None:
)

self.checked_add("timestamp", opus.Encoder.SAMPLES_PER_FRAME, 4294967295)

def played_time(self) -> datetime.timedelta:
"""Gets the elapsed time of the playing audio. Returns 0 if not playing anything."""
if self._player:
return datetime.timedelta(milliseconds=self._player.played_frames() * 20)
else:
return datetime.timedelta()