user1992705
user1992705

Reputation: 188

How do i find the duration of a .wav file in Python? Or how can I set a flag the moment the file stops playing?

Here's my code.

soundObj = pygame.mixer.Sound("burr.wav")
soundObj.play()
time.sleep(7) # 7 seconds for sound to play
soundObj.stop()

This current code allows me to play the sound for just 7 seconds even if it is 8 seconds long. Also, if the sound file is 4 seconds long, it will still sleep for 7 seconds. This is an issue.

I want the audio file to play for it's entire duration. No more, no less.

So I figure there are two ways of doing this.

  1. I could have some kind of flag that is set the moment the file finishes playing. How do I do this?

  2. Or, if I could somehow procure the duration of the sound file, I could replace time.sleep with time.sleep(duration). That would also solve my problem (I hope!).

So what do you think folks?

Upvotes: 0

Views: 2159

Answers (1)

Alexander
Alexander

Reputation: 12785

get_length() Will return object duration in seconds .

soundObj.get_length()

To know whether sound is playing , you should use Channel object .

voice = pygame.mixer.Channel(channel_num)
if voice.get_busy():
    # Code here ...

Upvotes: 1

Related Questions