import requests
import vlc
import tkinter as tk
from functools import partial
from PIL import ImageTk, Image
import io
class RadioPlayerApp:
def init(self):
self.player = None
self.volume_level = 50
self.window = tk.Tk()
self.window.title("Radio Stations")
self.create_gui()
self.window.mainloop()
def create_gui(self):
self.create_left_panel()
self.create_right_panel()
def create_left_panel(self):
self.left_panel = tk.Frame(self.window, bg="#f7f7f7", padx=20, pady=20)
self.left_panel.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.genre_label = tk.Label(
self.left_panel, text="Enter the music genre you wish to listen to:")
self.genre_label.pack(pady=10)
self.genre_entry = tk.Entry(self.left_panel)
self.genre_entry.pack()
self.country_label = tk.Label(
self.left_panel, text="Enter the country (optional) to filter the search results:")
self.country_label.pack(pady=10)
self.country_entry = tk.Entry(self.left_panel)
self.country_entry.pack()
self.search_button = tk.Button(
self.left_panel, text="Search", command=self.search_button_click, bg='lime green', activebackground='lime')
self.search_button.pack(pady=10)
self.create_stations_scrollable_canvas()
self.create_gui()
self.window.mainloop()
def create_gui(self):
self.create_left_panel()
self.create_right_panel()
def create_left_panel(self):
self.left_panel = tk.Frame(self.window, bg="#f7f7f7", padx=20, pady=20)
self.left_panel.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.genre_label = tk.Label(
self.left_panel, text="Enter the music genre you wish to listen to:")
self.genre_label.pack(pady=10)
self.genre_entry = tk.Entry(self.left_panel)
self.genre_entry.pack()
self.country_label = tk.Label(
self.left_panel, text="Enter the country (optional) to filter the search results:")
self.country_label.pack(pady=10)
self.country_entry = tk.Entry(self.left_panel)
self.country_entry.pack()
self.search_button = tk.Button(
self.left_panel, text="Search", command=self.search_button_click, bg='lime green', activebackground='lime')
self.search_button.pack(pady=10)
self.create_stations_scrollable_canvas()
def create_right_panel(self):
self.right_panel = tk.Frame(self.window, bg="#f7f7f7", padx=20, pady=20)
self.right_panel.pack(side=tk.RIGHT, fill=tk.BOTH)
self.now_playing_label = tk.Label(
self.right_panel, text="", font=("Arial", 14), bg="#f7f7f7")
self.now_playing_label.pack(pady=10)
self.artwork_label = tk.Label(self.right_panel, bg="#f7f7f7")
self.artwork_label.pack()
self.station_info_label = tk.Label(
self.right_panel, text="", font=("Arial", 12), bg="#f7f7f7", wraplength=300)
self.station_info_label.pack(pady=10)
self.now_playing_artwork_label = tk.Label(self.right_panel, bg="#f7f7f7")
self.now_playing_artwork_label.pack()
def create_stations_scrollable_canvas(self):
self.stations_canvas = tk.Canvas(self.left_panel)
self.stations_canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.stations_scrollbar = tk.Scrollbar(self.left_panel, orient="vertical",
command=self.stations_canvas.yview)
self.stations_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.stations_canvas.configure(yscrollcommand=self.stations_scrollbar.set)
self.stations_canvas.bind('<Configure>', lambda e: self.stations_canvas.configure(
scrollregion=self.stations_canvas.bbox("all")))
self.stations_frame = tk.Frame(self.stations_canvas)
self.stations_canvas.create_window(
(0, 0), window=self.stations_frame, anchor="nw")
def search_radio_stations(self, genre, country=None):
url = "https://de1.api.radio-browser.info/json/stations/bytagexact/"
params = {}
if genre:
url += genre
if country:
params["countrycode"] = country
response = requests.get(url, params=params)
if response.status_code == 200:
stations = response.json()
return stations
else:
print("Error searching radio stations:", response.status_code)
return None
play_button_bg = None
def play_station(self, station, play_button):
global play_button_bg
self.stop_playback()
self.player = vlc.MediaPlayer(station["url"])
self.player.play()
self.now_playing_label.config(text="Now Playing: " + station["name"])
if not play_button_bg:
play_button_bg = play_button["background"]
play_button["background"] = "red"
artwork_url = station.get("favicon", None)
if artwork_url:
try:
response = requests.get(artwork_url)
img_data = response.content
img = Image.open(io.BytesIO(img_data))
img = img.resize((150, 150), Image.LANCZOS)
artwork_image = ImageTk.PhotoImage(img)
self.artwork_label.config(image=artwork_image)
self.artwork_label.image = artwork_image
self.now_playing_artwork_label.config(image=artwork_image)
self.now_playing_artwork_label.image = artwork_image
except requests.exceptions.RequestException as e:
print("Error retrieving artwork image:", e)
station_info = f"Description: {station.get('description', 'N/A')}\nLocation: {station.get('country', 'N/A')}"
if station.get('homepage'):
station_info += f"\nWebsite: {station['homepage']}"
self.station_info_label.config(text=station_info)
def stop_playback(self, play_button):
if self.player is not None:
self.player.stop()
self.now_playing_label.config(text="")
self.artwork_label.config(image="")
self.station_info_label.config(text="")
self.now_playing_artwork_label.config(image="")
# Reset play button background to original color
play_button["background"] = self.play_button_bg
def set_volume(self, value):
self.volume_level = int(value)
if self.player is not None:
self.player.audio_set_volume(self.volume_level)
def search_button_click(self):
# Clear previous search results
for widget in self.stations_frame.winfo_children():
widget.destroy()
genre = self.genre_entry.get()
country = self.country_entry.get()
search_results = self.search_radio_stations(genre, country)
if search_results:
for i, station in enumerate(search_results[:10]):
station_name = station["name"]
station_url = station["url"]
station_frame = tk.Frame(self.stations_frame, bg="#f7f7f7", padx=20, pady=10)
station_frame.pack(fill=tk.BOTH, padx=10, pady=5)
station_frame.configure(borderwidth=1, relief=tk.RAISED)
image_frame = tk.Frame(station_frame, bg="#f7f7f7")
image_frame.pack(side=tk.LEFT)
station_image_label = tk.Label(image_frame, bg="#f7f7f7")
station_image_label.pack(side=tk.LEFT, padx=10)
station_label = tk.Label(station_frame, text=station_name, bg="#f7f7f7", font=("Arial", 12))
station_label.pack()
button_frame = tk.Frame(station_frame, bg="#f7f7f7")
button_frame.pack(pady=5)
play_button = tk.Button(button_frame, text="Play",
command=partial(self.play_station, station, play_button))
play_button.pack(side=tk.LEFT, padx=5)
stop_button = tk.Button(button_frame, text="Stop", command=self.stop_playback)
stop_button.pack(side=tk.LEFT, padx=5)
volume_scale = tk.Scale(button_frame, from_=0, to=100, orient=tk.HORIZONTAL,
command=self.set_volume)
volume_scale.set(self.volume_level)
volume_scale.pack(side=tk.LEFT, padx=5)
if i == 0:
self.play_station(station, play_button)
# Update the canvas scrollregion
self.stations_canvas.configure(scrollregion=self.stations_canvas.bbox("all"))
else:
print("No radio stations found for the given genre and country.")
if __name__ == "__main__":
app = RadioPlayerApp() >>>>"C:\Users\ra\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\ai radio\venv\Scripts\python.exe" "C:\Users\ra\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\ai radio\main.py"
Traceback (most recent call last):
File "C:\Users\ra\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\ai radio\main.py", line 8, in
class RadioPlayerApp:
File "C:\Users\ra\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\ai radio\main.py", line 224, in RadioPlayerApp
app = RadioPlayerApp()
^^^^^^^^^^^^^^
NameError: name 'RadioPlayerApp' is not defined
Process finished with exit code 1