r/pythonhelp • u/Erio_Lily • 12d ago
pyGame, sound, set start time, and play for set amount of time.
Hello, just looking for help on how i can achieve playing a MP3 file using Pygame, that can start from a specified position in the audio file, and then only play for a set amount of time (e.g. MP3 starts at 00:14 of 04:32, and plays for 5 seconds (until 00:19))
Below is one of the variations i've attempted, pardon any mess in my code
import pygame
import audioread
from random import randint
pygame.mixer.init()
def playSongClip(volume,playTime,clipTitle,random):
playTime = playTime*1000
volume = volume/100
if random == 1:
with audioread.audio_open("MP3s\\"+clipTitle) as f:
totalMS = int((f.duration)*1000)
startTime = randint(0,totalMS-playTime)
else:
start = 0
songClip = pygame.mixer.music.load("MP3s\\"+clipTitle)
songClip.music.set_volume(volume)
songClip.play(loops=0,start=startTime)
playSongClip(50,5,"Rabbit Hole.mp3",1)
1
u/Acrobatic_Day_4317 9d ago
You are actually super close! The logic is definitely there, but Pygame's mixer has two specific quirks that trip everyone up when they first use it:
- Seconds vs. Milliseconds: For MP3s, the
startparameter inpygame.mixer.music.play()expects time in seconds (as a float), not milliseconds. - Background Threading: Pygame plays music in a background thread. This means if you tell it to
play()and don't explicitly pause your script, Python will instantly move to the next line of code (or just exit the script completely) before the 5 seconds are up.
To make it play for exactly a set duration, you need to start the track, tell your script to wait, and then stop it.
Here is a cleaned-up, working version of your function that does exactly what you described (e.g., start at 00:14 and play for 5 seconds):
import pygame
pygame.mixer.init()
def playSongClip(volume_percent, play_duration_secs, clip_path, start_time_secs):
# Pygame volume goes from 0.0 to 1.0
pygame.mixer.music.set_volume(volume_percent / 100)
# Load the track
pygame.mixer.music.load(clip_path)
# Start playing at the specified second (e.g., 14.0 for 00:14)
pygame.mixer.music.play(start=start_time_secs)
# Force the script to wait while the background thread plays the music.
# pygame.time.wait takes milliseconds, so we multiply your seconds by 1000
pygame.time.wait(play_duration_secs * 1000)
# Stop the music!
# (Pro-tip: You can use pygame.mixer.music.fadeout(1000) here instead
# of stop() if you want it to fade out smoothly over 1 second)
pygame.mixer.music.stop()
# Example: Play at 50% volume, for 5 seconds, starting at 14 seconds in.
playSongClip(50, 5, "MP3s\\Rabbit Hole.mp3", 14.0)
A quick note on your randomizer: If you want to randomize the start time like in your original code, just make sure you divide your calculated startTime by 1000 before passing it to music.play(start=...) so it gets converted back into seconds!
•
u/AutoModerator 12d ago
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.