so basically i am creating a music downloader and player
what i am doing is searching files spotify client searching it with pytube downloading the highest qaulity stream and whenever i try to play that files using pygame or whatever it does not play saying its a bad stream its not a problem of pygame really whatever i play rather than that it plays no problem or so i thought maybe i should use qt webengine to play audio using audio tag in html but does not plays the downloaded file playing eveywhere else than python why can someone explain
this is the ss of when i try to the play the audio using web engine
this is the ss of when i try to the play the audio using web engine
this the error message when i try to play it with pygame or any else audio playing library
another one
note: i have tried all the pygame solutions none of them asked chatgpt not finded any solutio if you can help pls help here is the code
main.py
import sys
from PySide6.QtWidgets import (
QApplication,
QMainWindow,
QVBoxLayout,
QHBoxLayout,
QLabel,
QPushButton,
QWidget,
QFrame,
)
from PySide6.QtCore import (
Qt
)
from frames.player_frame import PlayerFrame
from frames.search_frame import SearchFrame
from frames.settings_frame import SettingsFrame
class VibeFlow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("VibeFlow Music")
self.setGeometry(1000, 500, 900, 600)
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QHBoxLayout(central_widget)
# Create menu frame
menu_frame = QFrame()
menu_layout = QVBoxLayout(menu_frame)
menu_frame.setFixedWidth(self.width() * 0.3)
# Create menu title
menu_title = QLabel("VibeFlow Music")
menu_title.setAlignment(Qt.AlignCenter)
menu_layout.addWidget(menu_title)
menu_layout.addStretch()
# Create menu buttons
button1 = QPushButton("Player")
button2 = QPushButton("Search")
button3 = QPushButton("Settings")
menu_frame.setStyleSheet("""
QPushButton{
font-size: 20px;
color: white;
background: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgb(61, 217, 245), stop:1 rgb(240, 53, 218));
font-family: Microsoft YaHei UI;
border-radius: 20px;
border: none;
padding: 9px;
margin: 8px;
}
QLabel{
font-size: 32px;
color: #ffffff;
font-family: Microsoft YaHei UI;
}
""")
menu_layout.addWidget(button1)
menu_layout.addWidget(button2)
menu_layout.addWidget(button3)
# Create content frame
self.content_frame = QFrame()
self.content_layout = QVBoxLayout(self.content_frame)
# Initializing frames
self.player_frame = PlayerFrame(self)
self.search_frame = SearchFrame(self)
self.settings_frame = SettingsFrame(self)
# Add frames to content_layout but hide them
self.content_layout.addWidget(self.player_frame)
self.content_layout.addWidget(self.search_frame)
self.content_layout.addWidget(self.settings_frame)
self.player_frame.hide()
self.search_frame.hide()
self.settings_frame.hide()
# Initially show only the player frame
self.player_frame.show()
# Add frames to main layout
layout.addWidget(menu_frame)
layout.addWidget(self.content_frame)
# Connect buttons to actions
button1.clicked.connect(self.button1_clicked)
button2.clicked.connect(self.button2_clicked)
button3.clicked.connect(self.button3_clicked)
def button1_clicked(self):
self.show_frame(self.player_frame)
def button2_clicked(self):
self.show_frame(self.search_frame)
def button3_clicked(self):
self.show_frame(self.settings_frame)
def show_frame(self, frame):
# Hide all frames first
for f in [self.player_frame, self.search_frame, self.settings_frame]:
f.hide()
# Show the selected frame
frame.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = VibeFlow()
window.show()
sys.exit(app.exec())
search-frame.py
from PySide6.QtWidgets import (
QVBoxLayout,
QHBoxLayout,
QLabel,
QPushButton,
QFrame,
QLineEdit,
QMainWindow,
QWidget,
QMessageBox,
)
from PySide6.QtCore import Qt,QSize
from PySide6.QtGui import QImage, QPixmap, QIcon
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import urllib.request
import json
from pytube import Search, YouTube
import os
sp = spotipy.Spotify(
auth_manager=SpotifyClientCredentials(
client_id="baf2d72ae6054acf91dca4f10f8e3f2e",
client_secret="aa9bbc0e087445a0a8799e676cd3ca5d",
)
)
class SearchFrame(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.layout = QVBoxLayout(self)
title_text = QLabel("Search Your Songs Here")
title_text.setAlignment(Qt.AlignCenter)
title_text.setStyleSheet(
"""
QLabel{
font-size: 30px;
color: #ffffff;
}
"""
)
self.layout.addWidget(title_text)
self.search_box = QLineEdit()
self.search_box.setPlaceholderText("Name of Song")
self.search_box.setStyleSheet(
"""
QLineEdit{
font-size: 15px;
color: #ffffff;
background-color: transparent;
border: 1px solid #fff;
border-radius: 12px;
padding: 5px;
}
"""
)
self.layout.addWidget(self.search_box)
search_button = QPushButton()
search_button.setIcon(QIcon("icons/icons8-search-96.png"))
search_button.setIconSize(QSize(40, 40))
search_button.setStyleSheet(
"""
QPushButton{
background-color: transparent;
border: none;
}
"""
)
search_button.clicked.connect(self.search_spotify)
self.layout.addWidget(search_button)
self.results = QFrame()
self.results_layout = QVBoxLayout(self.results)
self.results_layout.setAlignment(Qt.AlignTop)
self.layout.addWidget(self.results)
self.download_icon_label_list = []
def search_spotify(self):
term = self.search_box.text()
if term == "" or term is None:
return
else:
for i in reversed(range(self.results_layout.count())):
self.results_layout.itemAt(i).widget().deleteLater()
self.download_icon_label_list.clear()
self.results_layout.addWidget(QLabel("Search Results"))
data = sp.search(q=term, limit=10, type="track")
for idx, track in enumerate(data["tracks"]["items"]):
num = idx + 1
number_label = QLabel(f"{num}")
number_label.setStyleSheet(
"""
QLabel{
font-size: 18px;
color: #ffffff;
}
"""
)
self.results_layout.addWidget(number_label)
label_text = f"{track['name']}: {track['artists'][0]['name']}"
name_label = QLabel(label_text)
name_label.setStyleSheet(
"""
QLabel{
font-size: 18px;
color: #ffffff;
}
"""
)
self.results_layout.addWidget(name_label)
download_icon_label = QPushButton()
download_icon_label.setIcon(
QPixmap("icons/icons8-download-96.png").scaled(40, 40)
)
download_icon_label.setCursor(Qt.PointingHandCursor)
download_icon_label.clicked.connect(
lambda checked, index=idx: self.on_label_click(data, index)
)
self.results_layout.addWidget(download_icon_label)
self.download_icon_label_list.append(download_icon_label)
def download_complete(self, index, song_name, artist_name):
completed_icon = QPixmap("icons/icons8-complete-96.png").scaled(40, 40)
self.download_icon_label_list[index].setPixmap(completed_icon)
mp3_location = os.path.join(
"D:/Aadi/Downloads/music/", f"{song_name}.mp3"
)
cover_location = os.path.join(
"D:/Aadi/Downloads/music/", f"{song_name}.png"
)
with open("./songs.json", "r+") as f:
try:
songs_data = json.load(f)
except json.JSONDecodeError:
songs_data = []
new_song = {
"song_name": song_name,
"artist": artist_name,
"mp3_location": mp3_location,
"cover_location": cover_location,
}
songs_data.append(new_song)
f.seek(0)
json.dump(songs_data, f, indent=4)
def on_label_click(self, data, index):
QMessageBox.information(
self,
"Downloading",
f"Downloading {data['tracks']['items'][index]['name']} by {data['tracks']['items'][index]['artists'][0]['name']}",
)
song_name = data["tracks"]["items"][index]["name"]
artist_name = data["tracks"]["items"][index]["artists"][0]["name"]
filename = song_name + " by " + artist_name
cover_image = data["tracks"]["items"][index]["album"]["images"][0]["url"]
urllib.request.urlretrieve(
cover_image, f"D:/Aadi/Downloads/music/{filename}.png"
)
pytube_search = Search(filename)
final_url = "https://www.youtube.com/watch?v=" + pytube_search.results[0].video_id
pytube_download = YouTube(final_url)
pytube_download.streams.filter(progressive=True).first().download()
if __name__ == "__main__":
import sys
from PySide6.QtWidgets import QApplication
app = QApplication(sys.argv)
window = QMainWindow()
search_frame = SearchFrame()
window.setCentralWidget(search_frame)
window.setWindowTitle("VibeFlow Music")
window.setGeometry(1000, 500, 900, 600)
window.show()
sys.exit(app.exec())
i have tried every other way of playing audio its and i don’t have an idea why its happening pls help
Harneet Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1