Creating a timer that plays an MP3 file when it finishes can be done in various programming environments. Here are a few examples in different contexts:
1. Web (JavaScript + HTML) Example
You can create a simple countdown timer on a webpage that plays an MP3 sound when the timer reaches zero.
html
<!DOCTYPE html>
<html>
<head>
<title>Timer with MP3</title>
</head>
<body>
<h1>Timer with MP3</h1>
<input type="number" id="seconds" placeholder="Enter seconds" min="1" />
<button onclick="startTimer()">Start Timer</button>
<p id="countdown"></p>
<audio id="alarmSound" src="alarm.mp3" preload="auto"></audio>
<script>
function startTimer() {
const secondsInput = document.getElementById('seconds');
let time = parseInt(secondsInput.value);
if (isNaN(time) || time <= 0) {
alert("Please enter a valid number of seconds");
return;
}
const countdownEl = document.getElementById('countdown');
const alarmSound = document.getElementById('alarmSound');
countdownEl.textContent = time + " seconds remaining";
const interval = setInterval(() => {
time--;
if (time > 0) {
countdownEl.textContent = time + " seconds remaining";
} else {
clearInterval(interval);
countdownEl.textContent = "Time's up!";
alarmSound.play();
}
}, 1000);
}
</script>
</body>
</html>
Save this code as timer.html.
Place an alarm.mp3 file in the same folder or link to an online MP3.
Open in a browser and test.
2. Python Example with playsound
Python script that runs a timer and plays an MP3 when finished.
python
import time
from playsound import playsound
def timer(seconds, sound_file):
print(f"Timer started for {seconds} seconds.")
time.sleep(seconds)
print("Time's up!")
playsound(sound_file)
if __name__ == "__main__":
duration = int(input("Enter seconds for timer: "))
timer(duration, "alarm.mp3")
playsound can be installed with pip install playsound.
The script requires alarm.mp3 in the same directory or you can pass a full path to the MP3 file.
3. Using a Mobile App or Platform
If you want a timer app with an mp3 alarm, you can find many apps on iOS and Android markets that support custom alarm sounds.
If you want me to help create a timer with MP3 playback for a specific platform or language, let me know!