Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sqlite3
- import tempfile
- from tkinter import *
- from tkinter import filedialog, messagebox
- import vlc # Import the VLC module
- from PIL import Image, ImageTk, ImageSequence
- class Mp3PlayerApp:
- def __init__(self, root):
- self.root = root
- self.root.title("Najeeb Advanced MP3 Player")
- self.root.geometry("800x550")
- self.root.config(bg="lightblue")
- # VLC Media Player initialization
- self.vlc_instance = vlc.Instance()
- self.media_player = self.vlc_instance.media_player_new()
- # Database Setup
- self.db_connection = sqlite3.connect("DataMp3.db")
- self.cursor = self.db_connection.cursor()
- self.cursor.execute('''CREATE TABLE IF NOT EXISTS mp3_files
- (id INTEGER PRIMARY KEY, filename TEXT, filedata BLOB)''')
- self.db_connection.commit()
- # Frames for organization
- self.setup_frames()
- # Animated GIF Label
- self.load_gif(os.path.join(os.path.dirname(__file__), "MP3.gif"))
- # Player Controls
- self.setup_player_controls()
- # Volume Control
- self.setup_volume_control()
- # Audio Position Slider
- self.setup_position_slider()
- # Load MP3s from Database on Start
- self.load_mp3s_from_database()
- # Temporary files list
- self.temp_files = []
- def setup_frames(self):
- # Browse and Listbox Frame with Scrollbar
- browse_frame = Frame(self.root, bg="lightblue")
- browse_frame.pack(side=LEFT, fill=Y, padx=10, pady=10)
- # Listbox with Scrollbar
- self.listbox = Listbox(browse_frame, width=35, height=18)
- self.listbox.pack(side=TOP, pady=10)
- self.listbox.bind("<<ListboxSelect>>", self.play_selected_song)
- scrollbar = Scrollbar(browse_frame)
- scrollbar.pack(side=RIGHT, fill=Y)
- self.listbox.config(yscrollcommand=scrollbar.set)
- scrollbar.config(command=self.listbox.yview)
- # Buttons below Listbox
- button_frame = Frame(browse_frame, bg="lightblue")
- button_frame.pack(pady=10)
- browse_button = Button(button_frame, text="Browse Folder", command=self.browse_folder)
- browse_button.pack(fill=X, pady=2)
- add_button = Button(button_frame, text="Add MP3", command=self.add_mp3_file)
- add_button.pack(fill=X, pady=2)
- delete_button = Button(button_frame, text="Delete MP3", command=self.delete_mp3_file)
- delete_button.pack(fill=X, pady=2)
- save_button = Button(button_frame, text="Save MP3", command=self.save_mp3_to_database)
- save_button.pack(fill=X, pady=2)
- delete_temp_button = Button(button_frame, text="Delete Temporary Files", command=self.delete_temp_files)
- delete_temp_button.pack(fill=X, pady=2)
- def setup_player_controls(self):
- control_frame = Frame(self.root, bg="lightblue")
- control_frame.pack(pady=20)
- self.play_button = Button(control_frame, text="Play", command=self.play_selected_song)
- self.play_button.grid(row=0, column=0, padx=5)
- self.stop_button = Button(control_frame, text="Stop", command=self.stop_song)
- self.stop_button.grid(row=0, column=1, padx=5)
- prev_button = Button(control_frame, text="Previous", command=self.play_previous)
- prev_button.grid(row=0, column=2, padx=5)
- next_button = Button(control_frame, text="Next", command=self.play_next)
- next_button.grid(row=0, column=3, padx=5)
- def setup_volume_control(self):
- volume_frame = Frame(self.root, bg="lightblue")
- volume_frame.pack(pady=10)
- volume_label = Label(volume_frame, text="Volume", bg="lightblue")
- volume_label.pack(side=LEFT)
- self.volume_slider = Scale(volume_frame, from_=0, to=100, orient=HORIZONTAL, command=self.set_volume)
- self.volume_slider.set(50) # Set initial volume to 50%
- self.volume_slider.pack(side=LEFT)
- def setup_position_slider(self):
- self.position_slider = Scale(self.root, from_=0, to=100, orient=HORIZONTAL, command=self.seek_position)
- self.position_slider.pack(fill=X, padx=10, pady=10)
- def browse_folder(self):
- folder_path = filedialog.askdirectory()
- if not folder_path:
- return
- self.listbox.delete(0, END)
- self.cursor.execute("DELETE FROM mp3_files") # Clear previous entries
- self.db_connection.commit()
- for filename in os.listdir(folder_path):
- if filename.endswith(".mp3"):
- file_path = os.path.join(folder_path, filename)
- with open(file_path, 'rb') as file:
- file_data = file.read()
- self.cursor.execute("INSERT INTO mp3_files (filename, filedata) VALUES (?, ?)",
- (filename, file_data))
- self.listbox.insert(END, filename)
- self.db_connection.commit()
- messagebox.showinfo("Success", "MP3 files loaded from folder and saved to database!")
- def load_mp3s_from_database(self):
- self.listbox.delete(0, END)
- self.cursor.execute("SELECT filename FROM mp3_files")
- for row in self.cursor.fetchall():
- self.listbox.insert(END, row[0])
- def add_mp3_file(self):
- file_path = filedialog.askopenfilename(filetypes=[("MP3 files", "*.mp3")])
- if not file_path:
- return
- filename = os.path.basename(file_path)
- with open(file_path, 'rb') as file:
- file_data = file.read()
- self.cursor.execute("INSERT INTO mp3_files (filename, filedata) VALUES (?, ?)",
- (filename, file_data))
- self.db_connection.commit()
- self.listbox.insert(END, filename)
- messagebox.showinfo("Success", "MP3 file added to database!")
- def delete_mp3_file(self):
- selected_index = self.listbox.curselection()
- if not selected_index:
- return
- song_name = self.listbox.get(selected_index)
- self.cursor.execute("DELETE FROM mp3_files WHERE filename = ?", (song_name,))
- self.db_connection.commit()
- self.listbox.delete(selected_index)
- messagebox.showinfo("Deleted", "MP3 file deleted from database.")
- def save_mp3_to_database(self):
- selected_index = self.listbox.curselection()
- if not selected_index:
- messagebox.showwarning("Warning", "Please select an MP3 file to save.")
- return
- song_name = self.listbox.get(selected_index)
- folder_path = filedialog.askdirectory()
- if not folder_path:
- return
- self.cursor.execute("SELECT filedata FROM mp3_files WHERE filename = ?", (song_name,))
- file_data = self.cursor.fetchone()
- if file_data:
- file_path = os.path.join(folder_path, song_name)
- with open(file_path, 'wb') as file:
- file.write(file_data[0])
- messagebox.showinfo("Success", f"MP3 file '{song_name}' saved to selected folder.")
- else:
- messagebox.showerror("Error", "File data not found in database.")
- def play_selected_song(self, event=None):
- selected_index = self.listbox.curselection()
- if not selected_index:
- return
- song_name = self.listbox.get(selected_index)
- self.cursor.execute("SELECT filedata FROM mp3_files WHERE filename = ?", (song_name,))
- song_data = self.cursor.fetchone()[0]
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
- temp_file.write(song_data)
- temp_file_path = temp_file.name
- self.temp_files.append(temp_file_path)
- self.media_player.set_media(self.vlc_instance.media_new(temp_file_path))
- self.media_player.play()
- self.update_position_slider()
- def stop_song(self):
- self.media_player.stop()
- self.position_slider.set(0)
- def play_next(self):
- current_index = self.listbox.curselection()[0]
- next_index = (current_index + 1) % self.listbox.size()
- self.listbox.select_clear(current_index)
- self.listbox.select_set(next_index)
- self.play_selected_song()
- def play_previous(self):
- current_index = self.listbox.curselection()[0]
- prev_index = (current_index - 1) % self.listbox.size()
- self.listbox.select_clear(current_index)
- self.listbox.select_set(prev_index)
- self.play_selected_song()
- def set_volume(self, volume):
- self.media_player.audio_set_volume(int(volume))
- def update_position_slider(self):
- if self.media_player.is_playing():
- current_pos = self.media_player.get_time() // 1000
- self.position_slider.set(current_pos)
- self.root.after(1000, self.update_position_slider)
- def seek_position(self, position):
- self.media_player.set_time(int(position) * 1000)
- def delete_temp_files(self):
- # First, stop the media player to release any file handles
- self.media_player.stop()
- deleted_files = []
- for temp_file in self.temp_files:
- try:
- if os.path.exists(temp_file):
- os.remove(temp_file)
- deleted_files.append(temp_file)
- except Exception as e:
- messagebox.showerror("Error", f"Failed to delete {temp_file}: {e}")
- self.temp_files.clear() # Clear the list after deletion
- if deleted_files:
- messagebox.showinfo("Success", f"Temporary files deleted: {', '.join(deleted_files)}")
- else:
- messagebox.showinfo("Success", "No temporary files to delete.")
- def load_gif(self, gif_path):
- self.gif_label = Label(self.root)
- self.gif_label.pack()
- self.gif = Image.open(gif_path)
- self.frames = [ImageTk.PhotoImage(frame.copy()) for frame in ImageSequence.Iterator(self.gif)]
- self.gif_label.configure(image=self.frames[0])
- self.gif_index = 0
- self.update_gif()
- def update_gif(self):
- self.gif_index = (self.gif_index + 1) % len(self.frames)
- self.gif_label.configure(image=self.frames[self.gif_index])
- self.root.after(100, self.update_gif)
- if __name__ == "__main__":
- root = Tk()
- app = Mp3PlayerApp(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement