ClearCode

Music

Jul 23rd, 2022
1,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. import PySimpleGUI as sg
  2.  
  3. import base64
  4. from io import BytesIO
  5. from PIL import Image
  6.  
  7. from pygame import mixer, time
  8. mixer.init()
  9. clock = time.Clock()
  10.  
  11. def base64_image_import(path):
  12.     image = Image.open(path)
  13.     buffer = BytesIO()
  14.     image.save(buffer, format = 'PNG')
  15.     b64 = base64.b64encode(buffer.getvalue())
  16.     return b64
  17.  
  18. # import song
  19. path = sg.popup_get_file('Open', no_window = True)
  20. song_name = path.split('/')[-1].split('.')[0]
  21. song = mixer.Sound(path)
  22.  
  23. # timer
  24. song_length = int(song.get_length())
  25. time_since_start = 0
  26. pause_amount = 0
  27. playing = False
  28.  
  29. sg.theme('reddit')
  30.  
  31. play_layout = [
  32.     [sg.VPush()],
  33.     [sg.Push(),sg.Text(song_name, font = 'Arial 20'),sg.Push()],
  34.     [sg.VPush()],
  35.     [
  36.         sg.Push(),
  37.         sg.Button(image_data = base64_image_import('play.png'),key = '-PLAY-' ,button_color = 'white', border_width = 0),
  38.         sg.Text(' '),
  39.         sg.Button(image_data = base64_image_import('pause.png'),key = '-PAUSE-' ,button_color = 'white', border_width = 0),
  40.         sg.Push()
  41.     ],
  42.     [sg.VPush()],
  43.     [sg.Progress(song_length, size = (20,20), key = '-PROGRESS-')]
  44. ]
  45. volume_layout = [
  46.     [sg.VPush()],
  47.     [sg.Push(),sg.Slider(range = (0, 100), default_value = 100, orientation = 'h', key = '-VOLUME-'),sg.Push()],
  48.     [sg.VPush()],
  49. ]
  50.  
  51. layout = [
  52.     [sg.TabGroup([[sg.Tab('Play',play_layout),sg.Tab('Volume',volume_layout)]])]
  53. ]
  54.  
  55. window = sg.Window('Music Player',layout)
  56.  
  57. while True:
  58.     event, values = window.read(timeout = 1)
  59.     if event == sg.WIN_CLOSED:
  60.         break
  61.  
  62.     if playing:
  63.         time_since_start = time.get_ticks()
  64.         window['-PROGRESS-'].update((time_since_start - pause_amount) // 1000)
  65.  
  66.     if event == '-PLAY-':
  67.         playing = True
  68.         pause_amount += time.get_ticks() - time_since_start
  69.         if mixer.get_busy() == False:
  70.             song.play()
  71.         else:
  72.             mixer.unpause()
  73.  
  74.     if event == '-PAUSE-':
  75.         playing = False
  76.         mixer.pause()
  77.     song.set_volume(values['-VOLUME-'] / 100)
  78.  
  79. window.close()
  80.  
Add Comment
Please, Sign In to add comment