3

File playing normaly in python shell. Same code in script not playing, but getting right duration. What is wrong?

>>> import mplayer
>>> p = mplayer.Player()
>>> p.loadfile('announce_vlad.wav')

Script:

import mplayer
p = mplayer.Player()
p.loadfile('announce_vlad.wav')
print p.length
print p.is_alive()

Script output:

5.955873
True
user4968
  • 33
  • 3

1 Answers1

2

It seems that the player runs in the background, and stops as soon as your script exits. (In the Python shell, this won't normally be a problem, since the shell will stay open while waiting for your input.)

To keep the player from stopping prematurely, you'll need to somehow keep your script running until the player has finished. One way to do that, since you already know the duration of the clip you're playing, could be to just sleep() for the duration.

(There might be better ways to do that, but alas, I'm not really familiar enough with mplayer to tell. You may want to check the mplayer documentation to see if there's some way to make the player wake your script up when it has finished playing.)

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
  • ah, seconds :) `while p.stream_time_pos < p.stream_end: True` – user4968 Dec 05 '15 at 01:41
  • 1
    A busy loop like that will needlessly waste CPU time; basically, you'll have one CPU core running at 100%, doing nothing but repeatedly asking the player if it's done yet, probably hundreds of thousands of times per second. At least stick something like a `sleep(0.1)` inside the loop to reduce the CPU usage to only ten queries per second. – Ilmari Karonen Dec 05 '15 at 01:43
  • 1
    `sleep(p.length)` worked for me, without having to use a loop. Remember to do `from time import sleep` first. – tagawa Jan 03 '16 at 15:37