Audio¶
Sound plays audio,
Recorder records it, and
Audio.set_mode sets how the app's audio
relates to the device and other apps. Enable it with use = ["audio"] under
[plugins]. See the Audio guide.
Audio playback and recording.
Enable it in pythonnative.toml. Playback needs nothing else; recording also
needs a microphone usage string:
Example
from pythonnative.audio import Audio, Recorder, Sound
async def chime() -> None:
Audio.set_mode(category="playback")
sound = await Sound.load("https://example.com/chime.mp3")
sound.on_end(sound.unload)
sound.play()
async def voice_note() -> None:
recorder = await Recorder.start()
await asyncio.sleep(3)
recording = await recorder.stop()
async with await Sound.load(recording.path) as sound:
sound.play()
await asyncio.sleep(recording.duration)
Classes:
| Name | Description |
|---|---|
Recording |
A finished recording. |
Sound |
One loaded sound. Create it with |
Recorder |
A recording in progress. Create it with |
Audio |
App-wide audio behavior. |
Attributes:
| Name | Type | Description |
|---|---|---|
AudioCategory |
An |
|
RecordingQuality |
AAC in an MPEG-4 container at 16 kHz and 32 kbps, 22 kHz and 64 kbps, or 44.1 kHz and 128 kbps. |
|
SoundSource |
An |
AudioCategory
module-attribute
¶
AudioCategory = Literal[
"ambient",
"solo_ambient",
"playback",
"record",
"play_and_record",
"multi_route",
]
An AVAudioSession category, spelled in Python's style.
RecordingQuality
module-attribute
¶
RecordingQuality = Literal['low', 'medium', 'high']
AAC in an MPEG-4 container at 16 kHz and 32 kbps, 22 kHz and 64 kbps, or 44.1 kHz and 128 kbps.
SoundSource
module-attribute
¶
An http(s) URL, a file path, a bundled Asset, or a path under app/assets/.
Recording
dataclass
¶
Sound
¶
One loaded sound. Create it with Sound.load.
After a sound plays to its end, it rewinds to the start, so play()
plays it again. Playback pauses when the app goes to the background.
Using it as an async context manager unloads it on exit:
Raises:
| Type | Description |
|---|---|
NativeModuleError
|
From any method once the sound is unloaded, with
the code |
Methods:
| Name | Description |
|---|---|
load |
Load |
play |
Start or resume playback. |
pause |
Pause, keeping the position. |
stop |
Stop and rewind to the start. The sound stays loaded. |
seek |
Jump to |
set_volume |
Set the level from |
set_rate |
Set the speed, where |
on_end |
Call |
unload |
Release the player. Calling it again does nothing. |
Attributes:
| Name | Type | Description |
|---|---|---|
looping |
bool
|
Whether the sound restarts at its end. A looping sound doesn't call |
position |
float
|
The current position in seconds. |
duration |
float
|
The length in seconds, or |
looping
property
writable
¶
looping: bool
Whether the sound restarts at its end. A looping sound doesn't call on_end callbacks.
duration
property
¶
duration: float
The length in seconds, or 0 when it isn't known (a live stream).
load
async
classmethod
¶
load(
source: SoundSource,
*,
volume: float = 1.0,
rate: float = 1.0,
looping: bool = False
) -> "Sound"
Load source, ready to play. Playback doesn't need any permission.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
SoundSource
|
What to play: an |
required |
volume
|
float
|
Initial level from |
1.0
|
rate
|
float
|
Initial speed, where |
1.0
|
looping
|
bool
|
Whether to restart at the end. |
False
|
Returns:
| Type | Description |
|---|---|
'Sound'
|
The loaded sound. |
Raises:
| Type | Description |
|---|---|
NativeModuleError
|
With the code |
on_end
¶
Recorder
¶
Recorder(id: int)
A recording in progress. Create it with Recorder.start.
Methods:
| Name | Description |
|---|---|
start |
Prompt for the microphone if needed, and start recording. |
pause |
Pause recording. The paused time doesn't count toward the duration. |
resume |
Continue a paused recording in the same file. |
metering |
The current input level in decibels relative to full scale, from |
stop |
Finish the file. |
start
async
classmethod
¶
start(
path: Optional[str] = None,
quality: RecordingQuality = "high",
) -> "Recorder"
Prompt for the microphone if needed, and start recording.
Needs microphone under [permissions] in pythonnative.toml.
It isn't a build requirement of the plugin, since playback alone
doesn't use the microphone, so this is where a missing entry surfaces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Optional[str]
|
Where to write the |
None
|
quality
|
RecordingQuality
|
The encoding settings. |
'high'
|
Returns:
| Type | Description |
|---|---|
'Recorder'
|
The running recorder. |
Raises:
| Type | Description |
|---|---|
NativeModuleError
|
With the code |
metering
¶
metering() -> float
The current input level in decibels relative to full scale, from -160 (silence) to 0.
stop
async
¶
stop() -> Recording
Finish the file.
Returns:
| Type | Description |
|---|---|
Recording
|
The finished |
Raises:
| Type | Description |
|---|---|
NativeModuleError
|
With the code |
Audio
¶
App-wide audio behavior.
Methods:
| Name | Description |
|---|---|
set_mode |
Set how the app's audio relates to the device and to other apps. |
set_mode
staticmethod
¶
set_mode(
*,
category: AudioCategory = "playback",
mix_with_others: bool = False,
duck_others: bool = False,
default_to_speaker: bool = False,
allow_bluetooth: bool = False
) -> None
Set how the app's audio relates to the device and to other apps.
This is mainly iOS's AVAudioSession. Each call replaces the whole
mode. Without a call, iOS uses solo_ambient, which the silent
switch mutes; call this with category="playback" for sound the user
asked to hear. A running Recorder switches to play_and_record
and restores this mode when it stops.
Android has no categories, so category, default_to_speaker, and
allow_bluetooth have no effect there. mix_with_others and
duck_others control whether a Sound takes audio focus, which
pauses or lowers other apps. In the browser preview this does nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
category
|
AudioCategory
|
The |
'playback'
|
mix_with_others
|
bool
|
Play alongside other apps' audio. |
False
|
duck_others
|
bool
|
Play over other apps' audio, lowering it. |
False
|
default_to_speaker
|
bool
|
With |
False
|
allow_bluetooth
|
bool
|
With |
False
|
Raises:
| Type | Description |
|---|---|
NativeModuleError
|
With the code |