Play MP3s from Windows

A

Arlie

Hi,

Newbie here. I copied and pasted the code below. But when I ran it I
got this error:

D:\>python mp3.py
Duree du fichier : 298919 millisecondes
Traceback (most recent call last):
File "mp3.py", line 37, in <module>
time.sleep(int(buf)/1000)
ValueError: invalid literal for int() with base 10: ''

The code:

# -*- coding: utf-8 -*-

import time
from ctypes import windll, c_buffer

class mci:
def __init__(self):
self.w32mci = windll.winmm.mciSendStringA
self.w32mcierror = windll.winmm.mciGetErrorStringA

def send(self,commande):
buffer = c_buffer(255)
errorcode = self.w32mci(str(commande),buffer,254,0)
if errorcode:
return errorcode, self.get_error(errorcode)
else:
return errorcode,buffer.value

def get_error(self,error):
error = int(error)
buffer = c_buffer(255)
self.w32mcierror(error,buffer,254)
return buffer.value

def directsend(self, txt):
(err,buf)=self.send(txt)
if err != 0:
print'Erreur',str(err),'sur',txt,':',buf
return (err,buf)

mci=mci()
mci.directsend('open "d:\\Linger.mp3" alias toto')
mci.directsend('set toto time format milliseconds')
err,buf=mci.directsend('status toto length ')
print 'Duree du fichier : ',buf,' millisecondes'
err,buf=mci.directsend('play toto from 0 to '+str(buf))
time.sleep(int(buf)/1000)
mci.directsend('close toto')


#@-salutations
#--
#Michel Claveau

Please help. I'm in the middle of a project and I wanted to use this.
 
A

Arlie

Hi,

Newbie here. I copied and pasted the code below. But when I ran it I
got this error:

D:\>python mp3.py
Duree du fichier :  298919  millisecondes
Traceback (most recent call last):
  File "mp3.py", line 37, in <module>
    time.sleep(int(buf)/1000)
ValueError: invalid literal for int() with base 10: ''

The code:

# -*- coding: utf-8 -*-

import time
from ctypes import windll, c_buffer

class mci:
    def __init__(self):
        self.w32mci = windll.winmm.mciSendStringA
        self.w32mcierror = windll.winmm.mciGetErrorStringA

    def send(self,commande):
        buffer = c_buffer(255)
        errorcode = self.w32mci(str(commande),buffer,254,0)
        if errorcode:
            return errorcode, self.get_error(errorcode)
        else:
            return errorcode,buffer.value

    def get_error(self,error):
        error = int(error)
        buffer = c_buffer(255)
        self.w32mcierror(error,buffer,254)
        return buffer.value

    def directsend(self, txt):
        (err,buf)=self.send(txt)
        if err != 0:
            print'Erreur',str(err),'sur',txt,':',buf
        return (err,buf)

mci=mci()
mci.directsend('open "d:\\Linger.mp3" alias toto')
mci.directsend('set toto time format milliseconds')
err,buf=mci.directsend('status toto length ')
print 'Duree du fichier : ',buf,' millisecondes'
err,buf=mci.directsend('play toto from 0 to '+str(buf))
time.sleep(int(buf)/1000)
mci.directsend('close toto')

#@-salutations
#--
#Michel Claveau

Please help. I'm in the middle of a project and I wanted to use this.

By the way I using Python 2.6.2.
 
T

Tim Harig

Hi,

Newbie here. I copied and pasted the code below. But when I ran it I
got this error:

D:\>python mp3.py
Duree du fichier : 298919 millisecondes
Traceback (most recent call last):
File "mp3.py", line 37, in <module>
time.sleep(int(buf)/1000)
ValueError: invalid literal for int() with base 10: ''

Did you mean time.sleep(int(buf/1000)) which will guarantee that the value
sent to sleep is an integer rather then a float if buf is not evenly
divisible by 1000 (even after it has been rounded to an integer)?
 
T

Tim Harig

print 'Duree du fichier : ',buf,' millisecondes'

You can obviously make sure that 'buf' can be accessed as a string.
time.sleep(int(buf)/1000)

The error seems to be having issues converting buf to an int. Could you
possibly convert it to a string before converting it to an int?

time.sleep(int(str(buf))/1000)
 
M

MRAB

Arlie said:
Hi,

Newbie here. I copied and pasted the code below. But when I ran it I
got this error:

D:\>python mp3.py
Duree du fichier : 298919 millisecondes
Traceback (most recent call last):
File "mp3.py", line 37, in <module>
time.sleep(int(buf)/1000)
ValueError: invalid literal for int() with base 10: ''

The code:

# -*- coding: utf-8 -*-

import time
from ctypes import windll, c_buffer

class mci:
def __init__(self):
self.w32mci = windll.winmm.mciSendStringA
self.w32mcierror = windll.winmm.mciGetErrorStringA

def send(self,commande):
buffer = c_buffer(255)
errorcode = self.w32mci(str(commande),buffer,254,0)
if errorcode:
return errorcode, self.get_error(errorcode)
else:
return errorcode,buffer.value

def get_error(self,error):
error = int(error)
buffer = c_buffer(255)
self.w32mcierror(error,buffer,254)
return buffer.value

def directsend(self, txt):
(err,buf)=self.send(txt)
if err != 0:
print'Erreur',str(err),'sur',txt,':',buf
return (err,buf)

mci=mci()
mci.directsend('open "d:\\Linger.mp3" alias toto')
mci.directsend('set toto time format milliseconds')
err,buf=mci.directsend('status toto length ')
print 'Duree du fichier : ',buf,' millisecondes'

From the output it's clear that 'buf' contains '298919'.
err,buf=mci.directsend('play toto from 0 to '+str(buf))
time.sleep(int(buf)/1000)

From the output it's clear that 'buf' contains ''. Are you expecting it
to contain the length, like it did for the previous call? Perhaps you
should just use the length returned by the previous call.
 
A

Arlie

You can obviously make sure that 'buf' can be accessed as a string.


The error seems to be having issues converting buf to an int.  Could you
possibly convert it to a string before converting it to an int?

time.sleep(int(str(buf))/1000)

I'm still getting same error. So I just did this:

....

mci=mci()
mci.directsend('open "d:\\Linger.mp3" alias toto')
mci.directsend('set toto time format milliseconds')
err,buf=mci.directsend('status toto length ')
print 'Duree du fichier : ',buf,' millisecondes'
soundlength = int(buf) / 1000
err,buf=mci.directsend('play toto from 0 to '+str(buf))
#time.sleep(int(buf)/1000)
time.sleep(soundlength)
mci.directsend('close toto')
 
A

Arlie

 From the output it's clear that 'buf' contains '298919'.


 From the output it's clear that 'buf' contains ''. Are you expecting it
to contain the length, like it did for the previous call? Perhaps you
should just use the length returned by the previous call.

I actually did that and it's working fine now. Thanks.

I just got the program from the web. I have never program in python
but I'm taking the risk of replacing an existing perl program for a
more readable code. As of now everything seems like a haze to me. I
have read python tutorial just yesterday. I'm due to deliver this
project in 36 hours or less. This is suppose to download mp3 file to
client then connect to mysql get the person info and play appropriate
mp3 for that person calling his full name.
 
M

Michel Claveau - MVP

Hi!

I like if you like one of my (old) code ;-)

For your problem, Tim Harig have a good way.
Or, also, you can replace the line:
err,buf=mci.directsend('play toto from 0 to '+str(buf))
by:
mci.directsend('play toto from 0 to '+str(buf))

@-salutations
--
Michel Claveau
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top