Wed Jul 04, 2012 2:47 pm
Here's the code for the media player with keystroke detection, unfortunately it plays slowly (anyone any ideas):
[code]
import os
import sys
import pygame
import curses
#pygame.init()
loop = 0
esc = 0
os.system('clear')
# music files (ogg) must be in a folder, 'music', in same folder as program
os.system('ls -1pR music > files.out')
# open list of files
f = open('files.out', 'r')
# create a list C[ ]
C = ['this','is','a','list']
# empty c
C[:] = []
# read first line of files.out
A = f.readline()
# reads all lines of files.out, places full path to each file in C[ ]
while esc == 0:
if A[-2:-1] == ":":
while loop == 0:
B = f.readline()
if B[-2:-1] == ":":
A = B
if B[-5:-1] == ".ogg":
C[len(C)+1:] = [A[:-2] + '/' + B[:-1]]
if B == "":
esc = 1
loop = 1
else:
A = f.readline()
#init the curses screen
stdscr = curses.initscr()
#use cbreak to not require a return key press
curses.cbreak()
track = 0
breaker = 0
# set up the mixer
freq = 44100 # audio CD quality
bitsize = -16 # unsigned 16 bit
channels = 2 # 1 is mono, 2 is stereo
buffer = 2048 # number of samples (experiment to get right sound)
pygame.mixer.init(freq, bitsize, channels, buffer)
# optional volume 0 to 1.0
pygame.mixer.music.set_volume(1.0)
# starts pygame clock
clock = pygame.time.Clock()
while breaker == 0:
pygame.mixer.music.load(C[track])
pygame.mixer.music.play()
pz = 1
# check if playback has finished
while pygame.mixer.music.get_busy():
clock.tick(30)
# python curses to 'get' keyboard input
k = stdscr.getch()
# press b to pause/play track
if curses.keyname(k)=="b":
if pz == 1:
pygame.mixer.music.pause()
pz = 2
else:
pygame.mixer.music.play()
pz = 1
# press n to move to next track
if curses.keyname(k)=="n":
pygame.mixer.music.stop()
# press v to go back a track
if curses.keyname(k)=="v":
track = track -2
pygame.mixer.music.stop()
# press q to quit
if curses.keyname(k)=="q":
pygame.mixer.quit()
breaker = 1
curses.endwin()
track = track + 1
# check track number isn't less than first track
if track < 0:
track = len(C) + track
# check track number isn't more than last track
if track > len(C) - 1:
track = 0
[/code]
It plays ogg files, anywhere from within the 'music' folder that is in the same folder as the program.
Any improvements to the current script greatly appreciated. Requires pygame.
Aim of the program is a command line jukebox with keystroke detection for moving through tracks.
Matthew