发现python写一些简单的应用真的很方便。
这个播放器使用的库是pymedia,它的网址是:http://pymedia.org/
其实,这个播放器是根据其两个例子稍作修改而成的,基本上没有什么技术含量。这两个例子分别是:dump_wav.py和play_wav.py
倒是在安装pymedia的时候遇到一点麻烦,不知道为啥,LIBROOT/audio/libavcodec/dsputil.h总是编译出错,出错的函数是lrintf,后来发现好像在其它地方也没用到这个函数,于是把这个函数相关的代码删除,竟然奇迹般地编译通过了。
源码如下:
#! /usr/bin/python
'''
file: mp3player.py
author: rare
date: 2008/12/12
email: dux003#163.com
'''
def playMP3(name):
import pymedia.audio.acodec as acodec
import pymedia.muxer as muxer
import pymedia.audio.sound as sound
import time, wave, string, os
name1 = str.split(name, '.')
# Open demuxer first
dm = muxer.Demuxer(name1[-1].lower())
dec = None
snd = None
s = " "
f = open(name, 'rb')
while len(s):
s = f.read(20000)
if len(s):
frames = dm.parse(s)
for fr in frames:
if dec == None:
# Open decoder
dec = acodec.Decoder(dm.streams[0])
r = dec.decode(fr[1])
if r and r.data:
if snd == None:
snd = sound.Output(r.sample_rate, r.channels, sound.AFMT_S16_LE)
snd.play(r.data)
if type(snd) == 'pymedia.audio.sound.Output':
while snd.isPlaying():
time.sleep(0.05)
#-----------------------------------------------------------------
import sys
if len(sys.argv) != 2:
print "Usage: mp3player
else:
playMP3(sys.argv[1])
#run it like this
#python mp3player.py test.mp3