Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import pygame
- from Tkinter import *
- import os
- BAD_NAMES = ['Thumbs.db', 'Thumbs(1).db']
- script_path = os.path.dirname(os.path.realpath(__file__))
- pygame.mixer.pre_init(44100, -16, 2, 2048)
- pygame.init()
- root = Tk()
- def encoding(string):
- return unicode(string, "cp1251").encode("utf-8")
- def decoding(string):
- return string.encode("cp1251")
- def playmusic(soundfile):
- pygame.init()
- pygame.mixer.init()
- pygame.mixer.music.load(soundfile)
- pygame.mixer.music.play()
- def stopmusic():
- pygame.mixer.music.stop()
- def zeroes(n, num):
- if (len(str(num)) >= n):
- return str(num)
- else:
- return '0' * (n - len(str(num))) + str(num)
- def view(time):
- return zeroes(2, time / 60) + ':' + zeroes(2, time % 60)
- def curr_time():
- return int(round(float(pygame.mixer.music.get_pos()) / 1000))
- def play_time(add, duration):
- current = add + curr_time()
- return view(current) + ' / ' + view(duration)
- def getmixerargs():
- pygame.mixer.init()
- freq, size, chan = pygame.mixer.get_init()
- return freq, size, chan
- def initMixer():
- BUFFER = 2048
- FREQ, SIZE, CHAN = getmixerargs()
- pygame.mixer.init(FREQ, SIZE, CHAN, BUFFER)
- class Menu:
- def __init__(self, path, root, player):
- self.path = path
- self.player = player
- self.meow = os.listdir(os.path.join(script_path, path))
- self.toolbar = Frame(root)
- self.root = root
- if self.path != "data":
- self.back = Button(self.toolbar, text="Back", width=20)
- self.back.bind('<Button-1>', self.coming_back)
- self.back.pack(side=TOP, padx=2, pady=2)
- for i in range(len(self.meow)):
- if self.meow[i] not in BAD_NAMES:
- but = Button(self.toolbar, width=50)
- but["text"] = (encoding(self.meow[i][:-4]) if self.meow[i][-4:]
- in ['.mp3', '.ogg'] else encoding(self.meow[i]))
- but.bind('<Button-1>', self.callback if self.meow[i][-4:]
- not in ['.mp3', '.ogg'] else self.play)
- but.pack(side=BOTTOM, padx=2, pady=2)
- self.toolbar.pack(side=LEFT, fill=X)
- def callback(self, event):
- print(event.widget["text"])
- Menu(os.path.join(self.path, decoding(event.widget["text"])),
- self.root, self.player)
- self.destroy()
- def play(self, event):
- print("play")
- self.player.set_path(os.path.join(script_path, self.path,
- decoding(event.widget["text"]) + '.ogg'))
- def coming_back(self, event):
- print("Coming Back")
- new_path = self.path[:self.path.rfind('\\')]
- Menu(new_path, self.root, self.player)
- self.destroy()
- def destroy(self):
- self.toolbar.destroy()
- class Player:
- def __init__(self, root):
- self.curr = None
- self.played = False
- self.frame = Frame(root)
- play = Button(self.frame, text='play',width=20)
- pause = Button(self.frame, text='pause', width=20)
- self.volume = Scale(self.frame, orient=VERTICAL, length=300,
- from_=0, to=100, showvalue=0)
- pygame.mixer.music.set_volume(1.0)
- self.time = Scale(self.frame, orient=HORIZONTAL, length=300,
- from_=0, to=0, showvalue=0)
- self.sec = 0
- self.add = 0
- self.pause = False
- self.timer = Label(self.frame, text=play_time(0, 0))
- play.bind('<Button-1>', self.callback1)
- pause.bind('<Button-1>', self.callback2)
- self.volume.bind('<ButtonRelease-1>', self.set_volume)
- self.time.bind('<ButtonRelease-1>', self.set_time)
- self.volume.pack(side=RIGHT)
- self.timer.pack(side=TOP, expand=True)
- self.time.pack(side=BOTTOM)
- play.pack(side=RIGHT, padx=2, pady=2)
- pause.pack(side=RIGHT, padx=2, pady=2)
- self.frame.pack(side=RIGHT)
- def callback1(self, event):
- if (self.curr != None and not self.played):
- self.pause = False
- print(self.curr)
- self.timer.configure(text='0 / ' + str(self.length))
- self.time.configure(to=self.length)
- pygame.mixer.music.unpause()
- self.timerupdate()
- self.played = True
- elif (self.curr != None and self.played):
- self.pause = False
- print("Unpause")
- self.timerupdate()
- pygame.mixer.music.unpause()
- else:
- print("No music")
- def callback2(self, event):
- print("Pause")
- self.pause = True
- pygame.mixer.music.pause()
- def set_path(self, new):
- pygame.mixer.music.stop()
- self.add = 0
- self.curr = new
- self.played = False
- self.sound = pygame.mixer.Sound(self.curr)
- self.length = int(round(self.sound.get_length()))
- self.time.configure(to=self.length)
- self.timer.configure(text='00:00 / ' + view(self.length))
- playmusic(self.curr)
- pygame.mixer.music.pause()
- self.pause = True
- self.time.set(0)
- def set_volume(self, event):
- pygame.mixer.music.set_volume(1 - (float(self.volume.get()) / 100))
- def timerupdate(self):
- if (curr_time() + self.add >= self.length):
- print("End")
- self.pause = True
- pygame.mixer.music.stop()
- self.time.set(0)
- self.add = 0
- self.timer.configure(text='00:00 / ' + view(self.length))
- self.set_path(self.curr)
- elif not self.pause:
- if (abs(self.time.get() - curr_time() - self.add) <= 1):
- self.time.set(curr_time() + self.add)
- self.timer.configure(text=play_time(self.add, self.length))
- self.frame.after(100, self.timerupdate)
- def set_time(self, event):
- if (self.curr != None):
- self.add = self.time.get()
- self.timer.configure(text=play_time(self.add, self.length))
- pygame.mixer.music.stop()
- pygame.mixer.music.play(0, self.time.get())
- if (self.pause):
- pygame.mixer.music.pause()
- player = Player(root)
- menu = Menu("data", root, player)
- root.title("TheBerryCats Player")
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement