Skip to main content

Overview

The audio utilities provide functions for playing audio files from Praydo’s bundled assets. These are used for adhan (call to prayer) and notification sounds.

playSound

Plays an audio file from the application’s asset bundle.
async function playSound(fileName: string): Promise<void>

Parameters

fileName
string
required
The name of the audio file to play (must be located in the assets/ directory)

Returns

promise
Promise<void>
A promise that resolves when the audio starts playing (not when it finishes)

Available Audio Files

Praydo includes three audio files in the assets/ directory:
FileDescriptionUsage
adhan-fajr.mp3Fajr (dawn) adhanPlayed at Fajr time when adhan is enabled
adhan-makkah.mp3Standard adhan (Makkah style)Played at Dhuhr, Asr, Maghrib, and Isha when adhan is enabled
solemn.mp3Subtle notification tonePlayed when adhan is disabled or for pre-prayer notifications

Example

import { playSound } from '$lib/sound';

// Play Fajr adhan
await playSound('adhan-fajr.mp3');

// Play standard adhan
await playSound('adhan-makkah.mp3');

// Play notification sound
await playSound('solemn.mp3');

Implementation Details

The function:
  1. Reads the audio file from the Tauri resource directory using @tauri-apps/plugin-fs
  2. Creates a Blob from the file data with MIME type audio/mpeg
  3. Generates an object URL for the Blob
  4. Creates an HTML5 Audio element
  5. Calls .play() on the audio element
const filePath = await readFile(`assets/${fileName}`, {
  baseDir: BaseDirectory.Resource,
});
const mimeType = 'audio/mpeg';
const blob = new Blob([new Uint8Array(filePath)], { type: mimeType });
const assetUrl = URL.createObjectURL(blob);
const audio = new Audio(assetUrl);
audio.play();
Audio files are bundled with the application in the src-tauri/assets/ directory and packaged as resources in the final build.

Usage in Notifications

The PrayerManager uses playSound() when sending notifications: Pre-prayer notifications (N minutes before):
playSound('solemn.mp3');
Exact prayer time notifications:
// For Fajr with adhan enabled
if (isAlertEnabled && prayerKey === 'fajr') {
  playSound('adhan-fajr.mp3');
}
// For other prayers with adhan enabled
else if (isAlertEnabled) {
  playSound('adhan-makkah.mp3');
}
// For all prayers with adhan disabled
else {
  playSound('solemn.mp3');
}

Platform Support

Audio playback works across all platforms:
  • Windows: Uses native WebView2 audio
  • macOS: Uses WebKit audio
  • Linux: Uses WebKitGTK audio
The audio plays using the system’s default audio output. Volume is controlled by the system volume settings.

Error Handling

The function does not explicitly handle errors. If the audio file is missing or playback fails, the promise will reject. In production, you may want to wrap calls in try/catch:
try {
  await playSound('adhan-makkah.mp3');
} catch (error) {
  console.error('Failed to play sound:', error);
  // Fallback behavior
}

Asset Management

To add new audio files:
  1. Place MP3 files in src-tauri/assets/
  2. Ensure they’re included in the bundle configuration in tauri.conf.json:
    {
      "bundle": {
        "resources": ["assets"]
      }
    }
    
  3. Call playSound('your-file.mp3')
Audio files should be in MP3 format. Other formats may not be supported across all platforms.

Build docs developers (and LLMs) love