-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpvm6.py
More file actions
116 lines (102 loc) · 3.73 KB
/
pvm6.py
File metadata and controls
116 lines (102 loc) · 3.73 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
# James Fehr
# pvm5.py
# MP3Player
import os
import pygame
import time
import threading
class AudioPlayer:
def __init__(self):
self.directory = ""
self.audio_files = []
self.current_index = -1
self.playing = False
# Initialize Pygame mixer
pygame.mixer.init()
def display_menu(self):
os.system('cls' if os.name == 'nt' else 'clear') # Clear the console for a clean display
print("--- Audio Player Menu ---")
if self.playing:
self.display_song_title()
print("1. Open Directory")
print("2. Display MP3 Files")
print("3. Play")
print("4. Rewind")
print("5. Stop")
print("6. Next")
print("0. Exit")
def open_directory(self):
self.directory = input("Enter the directory path containing the MP3 files: ")
if os.path.isdir(self.directory):
self.audio_files = [
os.path.join(self.directory, filename)
for filename in os.listdir(self.directory)
if filename.endswith('.mp3')
]
if self.audio_files:
print(f"Found {len(self.audio_files)} MP3 files.")
self.current_index = 0
else:
print("The selected directory does not contain any MP3 files.")
else:
print("Invalid directory path.")
def display_mp3_files(self):
os.system('cls' if os.name == 'nt' else 'clear') # Clear the console for a clean display
if self.audio_files:
print("\n--- MP3 Files ---")
for index, file in enumerate(self.audio_files):
print(f"{index+1}. {os.path.basename(file)}")
else:
print("No MP3 files available.")
input("Press Enter to continue...")
def play_audio(self):
if self.audio_files:
if pygame.mixer.music.get_busy():
pygame.mixer.music.unpause()
else:
pygame.mixer.music.load(self.audio_files[self.current_index])
pygame.mixer.music.play()
self.playing = True
def rewind_audio(self):
if self.audio_files and pygame.mixer.music.get_busy():
pygame.mixer.music.rewind()
def stop_audio(self):
if self.audio_files:
pygame.mixer.music.stop()
self.playing = False
def next_audio(self):
if self.audio_files:
pygame.mixer.music.stop()
self.current_index = (self.current_index + 1) % len(self.audio_files)
pygame.mixer.music.load(self.audio_files[self.current_index])
pygame.mixer.music.play()
self.playing = True
def display_song_title(self):
current_song = os.path.basename(self.audio_files[self.current_index])
print(f"Currently Playing: {current_song}")
def run(self):
while True:
self.display_menu()
choice = input("Enter your choice (0-6): ")
if choice == "1":
self.open_directory()
elif choice == "2":
self.display_mp3_files()
elif choice == "3":
self.play_audio()
elif choice == "4":
self.rewind_audio()
elif choice == "5":
self.stop_audio()
elif choice == "6":
self.next_audio()
elif choice == "0":
break
else:
print("Invalid choice. Please try again.")
pygame.mixer.quit()
def main():
audio_player = AudioPlayer()
audio_player.run()
if __name__ == "__main__":
main()