- Audio Select
- Examples
- Basic Audio Select
- Initial Value
- Custom Placeholder
- Interactive Preview Controls
- Listening for Changes
- Programmatic Control
- Dynamic File Lists
- Real-world Example: Audio Settings Panel
- Properties
- AudioFile Interface
- Events
- CSS Parts
- Methods
- Behavior Details
- Audio Playback
- Selection Behavior
- Play/Pause Button
- Accessibility
- Best Practices
- Browser Compatibility
- Related Components
- Parts
Audio Select
<zn-audio-select>
ZnAudioSelect
Audio selects let users pick an audio file from a menu and preview it.
Examples
Basic Audio Select
The audio select component combines a standard select dropdown with an integrated audio preview player. Use
the label attribute to give the select an accessible label, provide an array of audio files via
the files property, and control the selected value with the value attribute.
<zn-audio-select id="basic-audio" label="Select an audio file" placeholder="Choose a sound" > </zn-audio-select> <script type="module"> const audioSelect = document.getElementById('basic-audio'); audioSelect.files = [ { name: 'Notification Sound', url: 'https://upload.wikimedia.org/wikipedia/commons/transcoded/3/34/Sound_Effect_-_Door_Bell.ogg/Sound_Effect_-_Door_Bell.ogg.mp3' }, { name: 'Alert Tone', url: 'https://cdn.pixabay.com/download/audio/2021/08/04/audio_12b0c7443c.mp3' }, { name: 'Chime', url: 'https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3' } ]; </script>
The play button inside the field lets users preview the selected audio file before confirming their choice. The audio automatically stops when a new selection is made or when the component is disconnected.
Initial Value
Use the value attribute to set an initial selection. The value should match one of the URLs in
your files array.
<zn-audio-select id="initial-value-audio" label="Select notification sound" placeholder="Choose a sound" > </zn-audio-select> <script type="module"> const audioSelect = document.getElementById('initial-value-audio'); audioSelect.files = [ { name: 'Notification Sound', url: 'https://upload.wikimedia.org/wikipedia/commons/transcoded/3/34/Sound_Effect_-_Door_Bell.ogg/Sound_Effect_-_Door_Bell.ogg.mp3' }, { name: 'Alert Tone', url: 'https://cdn.pixabay.com/download/audio/2021/08/04/audio_12b0c7443c.mp3' }, { name: 'Chime', url: 'https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3' } ]; // Set initial value audioSelect.value = 'https://cdn.pixabay.com/download/audio/2021/08/04/audio_12b0c7443c.mp3'; </script>
Custom Placeholder
Customize the placeholder text shown when no audio file is selected using the
placeholder attribute.
<zn-audio-select id="custom-placeholder-audio" label="Ringtone" placeholder="Pick your ringtone" > </zn-audio-select> <script type="module"> const audioSelect = document.getElementById('custom-placeholder-audio'); audioSelect.files = [ { name: 'Classic Ring', url: 'https://upload.wikimedia.org/wikipedia/commons/transcoded/3/34/Sound_Effect_-_Door_Bell.ogg/Sound_Effect_-_Door_Bell.ogg.mp3' }, { name: 'Modern Tone', url: 'https://cdn.pixabay.com/download/audio/2021/08/04/audio_12b0c7443c.mp3' }, { name: 'Gentle Chime', url: 'https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3' } ]; </script>
Interactive Preview Controls
The audio select includes an play/pause button inside the field. Click the play button to preview the currently selected audio. The button automatically changes to a pause icon while audio is playing.
- Select an audio file from the dropdown
- Click the play button (arrow icon) to start playback
- Click the pause button to stop playback
- Select a different file to automatically stop current playback
<zn-audio-select id="preview-demo-audio" label="Test audio playback" placeholder="Select to preview" > </zn-audio-select> <div style="margin-top: 1rem; padding: 1rem; background: var(--zn-color-neutral-100); border-radius: 4px;"> <strong>Instructions:</strong> <ol style="margin: 0.5rem 0 0 1.5rem; padding: 0;"> <li>Select an audio file from the dropdown</li> <li>Click the play button (arrow icon) to start playback</li> <li>Click the pause button to stop playback</li> <li>Select a different file to automatically stop current playback</li> </ol> </div> <script type="module"> const audioSelect = document.getElementById('preview-demo-audio'); audioSelect.files = [ { name: 'Short Beep', url: 'https://upload.wikimedia.org/wikipedia/commons/transcoded/3/34/Sound_Effect_-_Door_Bell.ogg/Sound_Effect_-_Door_Bell.ogg.mp3' }, { name: 'Notification Ping', url: 'https://cdn.pixabay.com/download/audio/2021/08/04/audio_12b0c7443c.mp3' }, { name: 'Alert Sound', url: 'https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3' } ]; </script>
Audio playback is prevented when no file is selected. The play/pause button becomes functional only after selecting an audio file from the dropdown.
Listening for Changes
The audio select emits a zn-change event when the selection changes. Access the selected URL
through the value property.
<zn-audio-select id="event-audio-select" label="Select your notification" placeholder="Choose one" > </zn-audio-select> <div style="margin-top: 1rem; padding: 1rem; background: var(--zn-color-neutral-100); border-radius: 4px;"> <strong>Selection:</strong> <div id="audio-selection-log" style="margin-top: 0.5rem; font-family: monospace; font-size: 0.875rem;"> No selection yet </div> </div> <script type="module"> const audioSelect = document.getElementById('event-audio-select'); const selectionLog = document.getElementById('audio-selection-log'); audioSelect.files = [ { name: 'Notification 1', url: 'https://upload.wikimedia.org/wikipedia/commons/transcoded/3/34/Sound_Effect_-_Door_Bell.ogg/Sound_Effect_-_Door_Bell.ogg.mp3' }, { name: 'Notification 2', url: 'https://cdn.pixabay.com/download/audio/2021/08/04/audio_12b0c7443c.mp3' }, { name: 'Notification 3', url: 'https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3' } ]; audioSelect.addEventListener('zn-change', (e) => { const selectedFile = audioSelect.files.find(f => f.url === e.target.value); if (selectedFile) { selectionLog.textContent = `Selected: ${selectedFile.name}\nURL: ${selectedFile.url}`; } else { selectionLog.textContent = 'No selection'; } }); </script>
Programmatic Control
Control the audio select programmatically by setting the value property or manipulating the
files array.
<zn-audio-select id="programmatic-audio" label="Programmable audio select" placeholder="Select a sound" > </zn-audio-select> <div style="margin-top: 1rem; display: flex; gap: 0.5rem; flex-wrap: wrap;"> <zn-button id="select-first">Select First</zn-button> <zn-button id="select-second">Select Second</zn-button> <zn-button id="clear-selection">Clear</zn-button> <zn-button id="add-file">Add File</zn-button> </div> <script type="module"> const audioSelect = document.getElementById('programmatic-audio'); const initialFiles = [ { name: 'Sound A', url: 'https://upload.wikimedia.org/wikipedia/commons/transcoded/3/34/Sound_Effect_-_Door_Bell.ogg/Sound_Effect_-_Door_Bell.ogg.mp3' }, { name: 'Sound B', url: 'https://cdn.pixabay.com/download/audio/2021/08/04/audio_12b0c7443c.mp3' } ]; audioSelect.files = [...initialFiles]; document.getElementById('select-first').addEventListener('click', () => { audioSelect.value = audioSelect.files[0]?.url || ''; }); document.getElementById('select-second').addEventListener('click', () => { audioSelect.value = audioSelect.files[1]?.url || ''; }); document.getElementById('clear-selection').addEventListener('click', () => { audioSelect.value = ''; }); document.getElementById('add-file').addEventListener('click', () => { const newFile = { name: `Sound ${audioSelect.files.length + 1}`, url: 'https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3' }; audioSelect.files = [...audioSelect.files, newFile]; }); </script>
Dynamic File Lists
Update the available audio files dynamically by setting the files property. This is useful for
loading files from an API or updating based on user actions.
<div class="form-spacing"> <zn-select id="category-selector" label="Audio category"> <zn-option value="">Select a category</zn-option> <zn-option value="notifications">Notifications</zn-option> <zn-option value="alarms">Alarms</zn-option> <zn-option value="ringtones">Ringtones</zn-option> </zn-select> <zn-audio-select id="dynamic-audio-select" label="Available sounds" placeholder="First select a category" > </zn-audio-select> </div> <script type="module"> const categorySelector = document.getElementById('category-selector'); const audioSelect = document.getElementById('dynamic-audio-select'); const audioCategories = { notifications: [ { name: 'Gentle Ping', url: 'https://upload.wikimedia.org/wikipedia/commons/transcoded/3/34/Sound_Effect_-_Door_Bell.ogg/Sound_Effect_-_Door_Bell.ogg.mp3' }, { name: 'Quick Beep', url: 'https://cdn.pixabay.com/download/audio/2021/08/04/audio_12b0c7443c.mp3' } ], alarms: [ { name: 'Wake Up Call', url: 'https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3' }, { name: 'Morning Alert', url: 'https://upload.wikimedia.org/wikipedia/commons/transcoded/3/34/Sound_Effect_-_Door_Bell.ogg/Sound_Effect_-_Door_Bell.ogg.mp3' } ], ringtones: [ { name: 'Classic Ring', url: 'https://cdn.pixabay.com/download/audio/2021/08/04/audio_12b0c7443c.mp3' }, { name: 'Modern Tone', url: 'https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3' } ] }; categorySelector.addEventListener('zn-change', (e) => { const category = e.target.value; if (category && audioCategories[category]) { audioSelect.files = audioCategories[category]; audioSelect.value = ''; audioSelect.placeholder = 'Select a sound'; } else { audioSelect.files = []; audioSelect.value = ''; audioSelect.placeholder = 'First select a category'; } }); </script>
Real-world Example: Audio Settings Panel
A complete example showing how to use the audio select in a settings interface for configuring notification sounds.
Audio Notification Settings
<div style="max-width: 600px; padding: 1.5rem; background: var(--zn-color-neutral-50); border-radius: 8px; border: 1px solid var(--zn-color-neutral-200);"> <h3 style="margin-top: 0;">Audio Notification Settings</h3> <form id="notification-settings-form" class="form-spacing"> <zn-checkbox id="enable-notifications" checked> Enable audio notifications </zn-checkbox> <zn-audio-select id="new-message-sound" label="New message" placeholder="Select a sound" > </zn-audio-select> <zn-audio-select id="mention-sound" label="@mention" placeholder="Select a sound" > </zn-audio-select> <zn-audio-select id="call-sound" label="Incoming call" placeholder="Select a sound" > </zn-audio-select> <div style="display: flex; gap: 0.5rem; margin-top: 1rem;"> <zn-button type="submit">Save Settings</zn-button> <zn-button type="reset">Reset</zn-button> </div> </form> </div> <script type="module"> const form = document.getElementById('notification-settings-form'); const enableNotifications = document.getElementById('enable-notifications'); const newMessageSound = document.getElementById('new-message-sound'); const mentionSound = document.getElementById('mention-sound'); const callSound = document.getElementById('call-sound'); // Sample audio files const audioFiles = [ { name: 'Gentle Ping', url: 'https://upload.wikimedia.org/wikipedia/commons/transcoded/3/34/Sound_Effect_-_Door_Bell.ogg/Sound_Effect_-_Door_Bell.ogg.mp3' }, { name: 'Quick Beep', url: 'https://cdn.pixabay.com/download/audio/2021/08/04/audio_12b0c7443c.mp3' }, { name: 'Soft Chime', url: 'https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3' } ]; // Set audio files for all selects [newMessageSound, mentionSound, callSound].forEach(select => { select.files = audioFiles; }); // Enable/disable audio selects based on checkbox function updateAudioSelectsState() { const enabled = enableNotifications.checked; [newMessageSound, mentionSound, callSound].forEach(select => { select.style.opacity = enabled ? '1' : '0.5'; select.style.pointerEvents = enabled ? 'auto' : 'none'; }); } enableNotifications.addEventListener('zn-change', updateAudioSelectsState); updateAudioSelectsState(); form.addEventListener('submit', (e) => { e.preventDefault(); if (!enableNotifications.checked) { alert('Audio notifications are disabled'); return; } const data = { newMessageSound: newMessageSound.value, mentionSound: mentionSound.value, callSound: callSound.value }; alert('Settings saved!\n\n' + JSON.stringify(data, null, 2)); }); </script>
Properties
| Property | Attribute | Description | Type | Default |
|---|---|---|---|---|
value |
value |
The currently selected audio file URL. When set, the audio player loads this URL for preview. | string |
'' |
label |
label |
The label text displayed above the select control. | string |
'' |
placeholder |
placeholder |
Placeholder text shown in the dropdown when no audio file is selected. | string |
'' |
files |
files |
Array of audio files available for selection. Each file object must have name (display
text) and url (audio file URL) properties.
|
AudioFile[] |
[] |
AudioFile Interface
interface AudioFile { name: string; // Display name shown in the dropdown url: string; // URL to the audio file }
Events
| Event | Description | Event Detail |
|---|---|---|
zn-change |
Emitted when the selected audio file changes. The event is triggered by the underlying
zn-select component and bubbles up. Access the new value via
event.target.value.
|
Inherited from zn-select |
zn-input |
Emitted when the select receives input. Inherited from the underlying
zn-select component.
|
Inherited from zn-select |
zn-focus |
Emitted when the select gains focus. | Inherited from zn-select |
zn-blur |
Emitted when the select loses focus. | Inherited from zn-select |
The audio select inherits all events from the zn-select component. Refer to the
select documentation for complete event details.
CSS Parts
| Part | Description |
|---|---|
preview |
The play/pause preview button. |
Methods
The audio select component provides these methods:
| Method | Description | Arguments | Returns |
|---|---|---|---|
stopAudio() |
Stops any currently playing audio, resets playback to the beginning, and updates the play/pause button state. | None | void |
Additional methods are available through the underlying zn-select component. Use standard DOM
methods like querySelector() to access the internal select and call its methods (e.g.,
focus(), blur(), show(), hide()).
Behavior Details
Audio Playback
- The audio preview player is implemented using the HTML5
AudioAPI - Clicking play loads and plays the currently selected audio file
-
Audio automatically stops when:
- The user clicks the pause button
- A different audio file is selected
- The component is disconnected from the DOM
- If audio playback fails (e.g., invalid URL, network error), an error is logged to the console and the button returns to the play state
- Attempting to play without selecting a file is prevented
Selection Behavior
-
The component automatically creates a disabled placeholder option from the
placeholderproperty - Each file in the
filesarray generates a correspondingzn-optionelement -
When
valuechanges, any playing audio is automatically stopped and the new audio URL is loaded -
The internal
zn-selecthandles all dropdown behavior, keyboard navigation, and accessibility features
Play/Pause Button
- Implemented as a
zn-checkboxin the prefix slot - Shows a play icon (arrow) when audio is not playing
- Shows a pause icon when audio is playing
- Uses primary color when checked (playing) and secondary color when unchecked (stopped)
- Button state is synchronized with audio playback state
Accessibility
The audio select component inherits all accessibility features from the underlying
zn-select component:
- Proper ARIA labels and roles for screen readers
- Full keyboard navigation support
- Focus management
- High contrast mode support
The play/pause checkbox should include an aria-label for screen reader users (this is
implemented by default in the component).
Best Practices
- File URLs: Ensure all audio file URLs are accessible and use HTTPS for security
- File Formats: Use widely supported audio formats (MP3, OGG, WAV) for maximum browser compatibility
-
File Names: Provide clear, descriptive names in the
nameproperty for better user experience - File Size: Keep audio files small for quick preview loading (notification sounds should typically be under 5 seconds)
- Error Handling: Audio playback errors are logged to the console but not shown to users; consider adding custom error handling for production use
- Performance: The component stops audio when disconnected, preventing memory leaks
-
Accessibility: Always provide a meaningful
labelattribute for screen reader users
Browser Compatibility
The audio select component requires:
- Modern browser with Web Components support
- HTML5 Audio API support
- All browsers supported by the base Zinc component library
Related Components
- Select - The underlying select component
- Checkbox - Used for the play/pause button
- Option - Used for dropdown options
Parts
| Name | Description |
|---|---|
preview |
The play/pause preview button. |