posted on: 2008-05-25 11:00:19
This mpg player is based on curses, threading,subprocess and pygst. It uses gst to play the music. Threads, and curses for controls. I use it to ssh in to a computer and play music. I recently changed it to take file names from the command line and to

>This mpg player is based on curses, threading and pygstplaybin tutorial from pygst. It uses gst to play the music. Threads, and curses for controls. I uses it to ssh in to a computer and play music. If your terminal doesn't support colors you have to disable the colors. Curses will complain.

#! /usr/bin/env python

""" ver 0.6 last modified 5/25/2008
this is a command line mp3 player  based on playbin
 """

import pygst
pygst.require("0.10")
import gst
import gtk
import threading
from random import randint
import sys
from subprocess import Popen,PIPE
import curses
from os import path

class mp3():
	def __init__(self,songlist):
		self.songs = songlist
		self.index = 0
		self.volume = 1
		self.title = 'unknown'
		self.artist = 'unknown'
		self.player = gst.element_factory_make("playbin", "player")
		fakesink = gst.element_factory_make('fakesink', "my-fakesink")
		self.player.set_property("video-sink", fakesink)
		bus = self.player.get_bus()
		bus.add_signal_watch()
		bus.connect('message',self.onmessage)
			
	def onmessage(self,bus,message):
		if message.type == gst.MESSAGE_EOS:
			self.index += 1
			Display.newsong()
		if message.type == gst.MESSAGE_TAG:
			dogs = message.parse_tag()

class controls(threading.Thread):
	def run(self):
		test = True
		Display.newsong()
		while test:
			ch = Display.win.getch()
			if ch==ord('f'):
				mpgplayer.index += 1
				Display.newsong()
			elif ch==ord('b'):
				mpgplayer.index -= 1
				Display.newsong()
			elif ch==ord('q'):
				mpgplayer.player.set_state(gst.STATE_NULL)
				test=False
				gtk.main_quit()
			elif ch==ord('u'):
				mpgplayer.volume += 1
				log = Popen(['amixer', 'set', 'Master', '%i'%mpgplayer.volume],stdout=PIPE)
				Display.win.addstr(7,20,'%2i'%mpgplayer.volume)
			elif ch==ord('d'):
				mpgplayer.volume -= 1
				log = Popen(['amixer', 'set', 'Master', '%i'%mpgplayer.volume],stdout=PIPE)
				Display.win.addstr(7,20,'%2i'%mpgplayer.volume)	
			elif ch==ord('p'):
				mpgplayer.player.set_state(gst.STATE_PAUSED)
				Display.win.addstr(5,20,"paused 'r' to resume",curses.color_pair(2))
			elif ch==ord('r'):
				mpgplayer.player.set_state(gst.STATE_PLAYING)
				Display.win.addstr(5,20,"                    ")
			else:
				Display.win.addstr(5,20,"wrong key, hit any key",curses.color_pair(1))
				Display.win.getch()
				Display.win.addstr(5,20,"                      ")
		Display.die()
	
	
class display():
	def __init__(self):
		self.stdscrn = curses.initscr()
		curses.noecho()
		curses.cbreak()
		curses.start_color()
		curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
		curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK)
		begin_x = 20 ; begin_y = 7
		height = 10 ; width = 60
		self.win = curses.newwin(height, width, begin_y, begin_x)
		self.win.refresh()
		self.win.addstr(1,3,"'f' forward 'b' previous and 'q' quit")
		self.win.addstr(3,3,"volume: 'u' up 'd' down")
	
	def newsong(self):
		if mpgplayer.index>=len(mpgplayer.songs):
			mpgplayer.index = 0
		elif mpgplayer.index<0:
			mpgplayer.index =len(mpgplayer.songs) - 1
		mpgplayer.player.set_state(gst.STATE_NULL)
		mpgplayer.player.set_property('uri',fileuri)
		mpgplayer.player.set_state(gst.STATE_PLAYING)
		filename = fileuri.split('/')[-1]
		if len(filename)<55:
			dispname = filename + ' '*(55 - len(filename))
		else:
			dispname = filename[0:55]
		self.win.addstr(9,3,dispname)
		self.win.refresh()
	
	def die(self):
		curses.nocbreak()
		self.stdscrn.keypad(0)
		curses.echo()
		curses.endwin()

urllist = ['file://'+ path.abspath(sys.argv[i]) for i in range(1,len(sys.argv))]
songnum = len(urllist)
newlist = []
while len(newlist)<songnum:
	randindex = randint(0,len(urllist)-1)
	item = urllist[randindex]
	newlist.append(item)
	urllist.remove(item)
log = Popen(['amixer', 'set', 'Master', '1'],stdout=PIPE)
gtk.gdk.threads_init()
Display = display()
mpgplayer = mp3(newlist)
controls().start()
gtk.main()

Of course I haven't commented it. Notice the 'threads_init()'. Very nescessary, GTK has it's own threading and if you don't do that the other threads won't work very well.

The next phase of development, will be to add more command line options, like playlist files, random and repeat. And the mpg tags should be read to display relevant information....song title, artist, file information.

Comments

Name: