Advertisement
makut

Player

Mar 29th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.48 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import pygame
  4. from Tkinter import *
  5. import os
  6.  
  7. BAD_NAMES = ['Thumbs.db', 'Thumbs(1).db']
  8.  
  9. script_path = os.path.dirname(os.path.realpath(__file__))
  10.  
  11. pygame.mixer.pre_init(44100, -16, 2, 2048)
  12. pygame.init()
  13.  
  14. root = Tk()
  15.  
  16.  
  17. def encoding(string):
  18.     return unicode(string, "cp1251").encode("utf-8")
  19.  
  20.  
  21. def decoding(string):
  22.     return string.encode("cp1251")
  23.  
  24.  
  25. def playmusic(soundfile):
  26.     pygame.init()
  27.     pygame.mixer.init()
  28.     pygame.mixer.music.load(soundfile)
  29.     pygame.mixer.music.play()
  30.    
  31.  
  32. def stopmusic():
  33.     pygame.mixer.music.stop()
  34.  
  35. def zeroes(n, num):
  36.     if (len(str(num)) >= n):
  37.         return str(num)
  38.     else:
  39.         return '0' * (n - len(str(num))) + str(num)
  40.  
  41.  
  42. def view(time):
  43.     return zeroes(2, time / 60) + ':' + zeroes(2, time % 60)
  44.  
  45. def curr_time():
  46.     return int(round(float(pygame.mixer.music.get_pos()) / 1000))
  47.  
  48. def play_time(add, duration):
  49.     current = add + curr_time()
  50.     return view(current) + ' / ' + view(duration)
  51.  
  52.  
  53. def getmixerargs():
  54.     pygame.mixer.init()
  55.     freq, size, chan = pygame.mixer.get_init()
  56.     return freq, size, chan
  57.  
  58.  
  59. def initMixer():
  60.     BUFFER = 2048
  61.     FREQ, SIZE, CHAN = getmixerargs()
  62.     pygame.mixer.init(FREQ, SIZE, CHAN, BUFFER)
  63.  
  64.  
  65. class Menu:
  66.     def __init__(self, path, root, player):
  67.         self.path = path
  68.         self.player = player
  69.         self.meow = os.listdir(os.path.join(script_path, path))
  70.         self.toolbar = Frame(root)
  71.         self.root = root
  72.         if self.path != "data":
  73.             self.back = Button(self.toolbar, text="Back", width=20)
  74.             self.back.bind('<Button-1>', self.coming_back)
  75.             self.back.pack(side=TOP, padx=2, pady=2)
  76.         for i in range(len(self.meow)):
  77.             if self.meow[i] not in BAD_NAMES:
  78.                 but = Button(self.toolbar, width=50)
  79.                 but["text"] = (encoding(self.meow[i][:-4]) if self.meow[i][-4:]
  80.                                in ['.mp3', '.ogg'] else encoding(self.meow[i]))
  81.                 but.bind('<Button-1>', self.callback if self.meow[i][-4:]
  82.                          not in ['.mp3', '.ogg'] else self.play)
  83.                 but.pack(side=BOTTOM, padx=2, pady=2)
  84.         self.toolbar.pack(side=LEFT, fill=X)
  85.        
  86.     def callback(self, event):
  87.         print(event.widget["text"])
  88.         Menu(os.path.join(self.path, decoding(event.widget["text"])),
  89.              self.root, self.player)
  90.         self.destroy()
  91.    
  92.     def play(self, event):
  93.         print("play")
  94.         self.player.set_path(os.path.join(script_path, self.path,
  95.                                           decoding(event.widget["text"]) + '.ogg'))
  96.        
  97.    
  98.     def coming_back(self, event):
  99.         print("Coming Back")
  100.         new_path = self.path[:self.path.rfind('\\')]
  101.         Menu(new_path, self.root, self.player)
  102.         self.destroy()
  103.    
  104.     def destroy(self):
  105.         self.toolbar.destroy()
  106.      
  107.  
  108. class Player:
  109.     def __init__(self, root):
  110.         self.curr = None
  111.         self.played = False
  112.         self.frame = Frame(root)
  113.         play = Button(self.frame, text='play',width=20)
  114.         pause = Button(self.frame, text='pause', width=20)
  115.         self.volume = Scale(self.frame, orient=VERTICAL, length=300,
  116.                             from_=0, to=100, showvalue=0)
  117.         pygame.mixer.music.set_volume(1.0)
  118.         self.time = Scale(self.frame, orient=HORIZONTAL, length=300,
  119.                           from_=0, to=0, showvalue=0)
  120.         self.sec = 0
  121.         self.add = 0
  122.         self.pause = False
  123.         self.timer = Label(self.frame, text=play_time(0, 0))
  124.         play.bind('<Button-1>', self.callback1)
  125.         pause.bind('<Button-1>', self.callback2)
  126.         self.volume.bind('<ButtonRelease-1>', self.set_volume)
  127.         self.time.bind('<ButtonRelease-1>', self.set_time)
  128.         self.volume.pack(side=RIGHT)
  129.         self.timer.pack(side=TOP, expand=True)
  130.         self.time.pack(side=BOTTOM)
  131.         play.pack(side=RIGHT, padx=2, pady=2)
  132.         pause.pack(side=RIGHT, padx=2, pady=2)
  133.         self.frame.pack(side=RIGHT)
  134.        
  135.     def callback1(self, event):
  136.         if (self.curr != None and not self.played):
  137.             self.pause = False
  138.             print(self.curr)
  139.             self.timer.configure(text='0 / ' + str(self.length))
  140.             self.time.configure(to=self.length)
  141.             pygame.mixer.music.unpause()
  142.             self.timerupdate()
  143.             self.played = True            
  144.         elif (self.curr != None and self.played):
  145.             self.pause = False
  146.             print("Unpause")
  147.             self.timerupdate()
  148.             pygame.mixer.music.unpause()
  149.         else:
  150.             print("No music")
  151.            
  152.     def callback2(self, event):
  153.         print("Pause")
  154.         self.pause = True
  155.         pygame.mixer.music.pause()
  156.        
  157.     def set_path(self, new):
  158.         pygame.mixer.music.stop()
  159.         self.add = 0
  160.         self.curr = new
  161.         self.played = False
  162.         self.sound = pygame.mixer.Sound(self.curr)
  163.         self.length = int(round(self.sound.get_length()))  
  164.         self.time.configure(to=self.length)
  165.         self.timer.configure(text='00:00 / ' + view(self.length))
  166.         playmusic(self.curr)
  167.         pygame.mixer.music.pause()
  168.         self.pause = True
  169.         self.time.set(0)
  170.    
  171.     def set_volume(self, event):
  172.         pygame.mixer.music.set_volume(1 - (float(self.volume.get()) / 100))
  173.    
  174.     def timerupdate(self):
  175.         if (curr_time() + self.add >= self.length):
  176.             print("End")
  177.             self.pause = True
  178.             pygame.mixer.music.stop()
  179.             self.time.set(0)
  180.             self.add = 0
  181.             self.timer.configure(text='00:00 / ' + view(self.length))
  182.             self.set_path(self.curr)
  183.         elif not self.pause:
  184.             if (abs(self.time.get() - curr_time() - self.add) <= 1):
  185.                 self.time.set(curr_time() + self.add)
  186.             self.timer.configure(text=play_time(self.add, self.length))
  187.             self.frame.after(100, self.timerupdate)
  188.    
  189.     def set_time(self, event):
  190.         if (self.curr != None):
  191.             self.add = self.time.get()
  192.             self.timer.configure(text=play_time(self.add, self.length))
  193.             pygame.mixer.music.stop()
  194.             pygame.mixer.music.play(0, self.time.get())
  195.             if (self.pause):
  196.                 pygame.mixer.music.pause()
  197.  
  198.  
  199. player = Player(root)
  200. menu = Menu("data", root, player)
  201. root.title("TheBerryCats Player")
  202. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement