forked from sd17spring/InteractiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmusic_player.py
More file actions
130 lines (106 loc) · 3.5 KB
/
music_player.py
File metadata and controls
130 lines (106 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import pygame
import multiprocessing
import os
import sys
import random
import threading
import time
class Music_Player():
def __init__(self):
pygame.init()
pygame.mixer.init()
self.display = pygame.display.set_caption('Music Player')
self.blue = (0, 0, 255)
self.size = [640,480]
self.screen = pygame.display.set_mode(self.size)
self.clock = pygame.time.Clock()
def filesfolder(self):
files = []
path = 'C:/Users/apayano/Documents/GitHub/InteractiveProgramming'
directory = os.listdir(path)
for filename in directory:
if filename.endswith(".wav"):
files.append(filename)
files.sort()
return files
def picksong(self):
song = '1.wav'
return song
#files = self.filesfolder()
#songnum = random.randint(0, 14)
#song = files[songnum]
#print(song)
#return song
def sound(self):
sound = pygame.mixer.Sound(self.picksong())
return sound
def playsound(self):
"""Play sound through default mixer channel in blocking manner.
This will load the whole sound into memory before playback.
"""
song = self.sound()
song.play()
#while pygame.mixer.get_busy():
# print ("Playing...")
# clock.tick(1000)
def stopsound(self):
song = self.sound()
song.stop()
#def playnext(self):
# i = self.files.index(self.song)
# pygame.mixer.Sound(self.files[i + 1])
# clock = pygame.time.Clock()
# sound.play()
# while pygame.mixer.get_busy():
# print ("Playing...")
# clock.tick(1000)
#def playprevious(self):
# pygame.mixer.Sound(files[self.i - 1])
# clock = pygame.time.Clock()
# sound.play()
# while pygame.mixer.get_busy():
# print ("Playing...")
# clock.tick(1000)
musicplayer = Music_Player()
sound = musicplayer.sound()
def events(player, sound):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
sound.play()
if event.key == pygame.K_s:
sound.stop()
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
player.screen.fill(player.blue)
pygame.display.update()
# def getmixerargs(self):
# pygame.mixer.init()
# freq, size, chan = pygame.mixer.get_init()
# return freq, size, chan
# def initMixer(self):
# BUFFER = 3072 # audio buffer size, number of samples since pygame 1.8.
# FREQ, SIZE, CHAN = getmixerargs()
# pygame.mixer.init(FREQ, SIZE, CHAN, BUFFER)
musicplayer = Music_Player()
files = musicplayer.filesfolder()
#print(files)
#playWAV = musicplayer.playsound()
print('press p - play sound')
print('press s - stop playing instantly')
events(musicplayer,sound)
# if __name__ == '__main__':
# t = threading.Thread(name='daemon', target= musicplayer.events)
# d = threading.Thread(name= 'non-daemon', target= musicplayer.playsound)
# t.setDaemon(True)
# d.start()
# t.start()
#if __name__ == "__main__":
# p1 = multiprocessing.Process(target= musicplayer.playsound, args())
#playWAV = musicplayer.playsound(input('WAV File:'))
#playsong = musicplayer.playsound()