juno.sound¶
Audio playback via AVAudioPlayer.
Two entry shapes:
play()is fire-and-forget — start an audio file playing and return immediately. The player releases itself automatically when playback finishes, so users don’t have to manage anything.Playeris the stateful form for scripts that need control over the playback lifecycle (pause / seek / volume). It’s a context manager; usewithblocks or callPlayer.close()explicitly to release the underlyingAVAudioPlayerdeterministically —__del__is a best-effort safety net only.
Typical usage:
from juno import sound
# Fire-and-forget
sound.play("/path/to/notification.wav")
# Stateful — full control
with sound.Player("/path/to/song.mp3") as p:
p.volume = 0.7
p.play()
time.sleep(2.0)
p.pause()
print(p.position, "of", p.duration)
p.seek(0)
p.play()
Notes:
Path validation is intentionally minimal at the Python layer —
AVAudioPlayersurfaces format / decoding errors directly.A
Playerinstance is not thread-safe by itself; a script driving onePlayerfrom multiple threads must add its own synchronisation.
- class juno.sound.Player(path)¶
Bases:
objectA long-lived
AVAudioPlayerwrapper.Construct with a filesystem path; the file is loaded immediately. Call
play()to start,pause()to pause without resetting,stop()to reset to position 0. The instance must be released by callingclose()(or via awithblock);__del__is a best-effort fallback. Forgetting to closePlayerinstances keeps the underlying audio resource alive until the script’s process exits.- Parameters:
path (str)
- close()¶
Release the underlying handle. Idempotent — calling twice is fine; the second call is a no-op.
- Return type:
None
- play()¶
Start (or resume from pause) playback.
- Raises:
RuntimeError – If the player is closed, or
AVAudioPlayer.play()rejected the call (e.g. audio session was claimed by another app).- Return type:
None
- pause()¶
Pause playback without resetting the position.
- Return type:
None
- stop()¶
Stop playback and reset the position to 0.
- Return type:
None
- seek(seconds)¶
Move the playhead to
seconds.Out-of-range values are clamped to
[0, duration]byAVAudioPlayersilently. Negative seconds are accepted on input (Apple’s behaviour) but typically clamp to 0.
- juno.sound.play(path)¶
Start playing
pathand return immediately.The native player keeps itself alive until playback finishes (or fails), at which point its handle is automatically released. Useful for short notification sounds where the caller doesn’t need to control the playback lifecycle.
- Parameters:
path (str) – Filesystem path to an audio file readable by
AVAudioPlayer(any of the formats Core Audio supports — m4a, mp3, wav, aac, …).- Raises:
TypeError – If
pathis not a string.ValueError – If
pathis empty.RuntimeError – If the file could not be loaded or playback could not start (Core Audio rejects the format, audio session unavailable, etc.).
- Return type:
None