Reputation: 1
I'm trying to make a music app using python for my 12th grade project but I'm getting an error which I have no idea how to fix. What I'm indenting to do is to make fully functional app-like python program with GUI and is able to play both online and offline music.
Initially I started with just a program in which we can search and find the first result which comes in Youtube and just play it, pause it and stop it. Which I later improved so that we can play upto the first 10 results in Youtube.I actually got the idea for the searching and finding part from stack overflow itself (which was very helpful btw.) and developed the rest of the code from it. We also can play/pause/stop the music too. But I wanted it to look even more good so...I added a GUI using tkinter which was pretty hard at first but soon it was easy enough. What I can do now is Type the music name in the Tkinter window and Search button (added the Search button so it will run the 'link-finding-and-playing-program' when I press it). The libraries I use now are:
CODE OF THE PROGRAM:
from tkinter import *
import re,requests, urllib.parse, urllib.request
import vlc
import pafy
root = Tk()
root.geometry('475x225'); root.title('PyMu6')
Entry = Entry(root, width=26,font=('Circular',13))
Entry.place(x=127,y=17)
def search():
music_name = Entry.get()
print(music_name)
if type(music_name)==str:
query_string = urllib.parse.urlencode({"search_query": music_name})
formatUrl = urllib.request.urlopen("https://www.youtube.com/results?" + query_string)
search_results = re.findall(r"watch\?v=(\S{11})", formatUrl.read().decode())
clip = requests.get("https://www.youtube.com/watch?v=" + "{}".format(search_results[0]))
clip1 = "https://www.youtube.com/watch?v=" + "{}".format(search_results[0])
clip2 = "https://www.youtube.com/watch?v=" + "{}".format(search_results[1])
clip3 = "https://www.youtube.com/watch?v=" + "{}".format(search_results[2])
clip4 = "https://www.youtube.com/watch?v=" + "{}".format(search_results[3])
clip5= "https://www.youtube.com/watch?v=" + "{}".format(search_results[4])
clip6 = "https://www.youtube.com/watch?v=" + "{}".format(search_results[5])
clip7 = "https://www.youtube.com/watch?v=" + "{}".format(search_results[6])
clip8 = "https://www.youtube.com/watch?v=" + "{}".format(search_results[7])
clip9 = "https://www.youtube.com/watch?v=" + "{}".format(search_results[8])
clip10 = "https://www.youtube.com/watch?v=" + "{}".format(search_results[9])
title1 = pafy.new(clip1)
title2 = pafy.new(clip2)
title3 = pafy.new(clip3)
title4 = pafy.new(clip4)
title5 = pafy.new(clip5)
title6 = pafy.new(clip6)
title7 = pafy.new(clip7)
title8 = pafy.new(clip8)
title9 = pafy.new(clip9)
title10 = pafy.new(clip10)
video1 = title1.title
video2 = title2.title
video3 = title3.title
video4 = title4.title
video5 = title5.title
video6 = title6.title
video7 = title7.title
video8 = title8.title
video9 = title9.title
video10 = title10.title
Label(root,text=video1,fg='blue').place(x=60,y=70)
print()
print('1)', video1)
print('2)', video2)
print('3)', video3)
print('4)', video4)
print('5)', video5)
print('6)', video6)
print('7)', video7)
print('8)', video8)
print('9)', video9)
print('10)', video10)
print()
e = input('Enter Song: ')
if e == '1':
print(clip1)
url = clip1
if e == '2':
print(clip2)
url = clip2
if e == '3':
print(clip2)
url = clip3
if e == '4':
print(clip4)
url = clip4
if e == '5':
print(clip5)
url = clip5
if e == '6':
print(clip6)
url = clip6
if e == '7':
print(clip7)
url = clip7
if e == '8':
print(clip8)
url = clip8
if e == '9':
print(clip9)
url = clip9
if e == '10':
print(clip10)
url = clip10
video = pafy.new(url)
best = video.getbest()
media = vlc.MediaPlayer(best.url)
titl = video.title
print(titl)
media.play()
def play():
media.play()
def pause():
media.pause()
def stop():
media.stop()
Search_button = Button(root, text = 'Search', width = 10, font = ('Circular.', 10),bg='#1DB954',command=MusicPlayer.search)
Play = Button(root, text = 'Play', width = 10,font = ('Circular.', 10),bg='#1DB954',command=play)
Pause = Button(root,text = 'Pause', width = 10, font = ('Circular.', 10),bg='#1DB954',command=pause)
Stop = Button(root,text = 'Stop', width = 10, font = ('Circular.',10),bg='#1DB954',command=stop)
Search_button.place(x=380,y=15);Play.place(x=120,y=170);Pause.place(x=220,y=170);Stop.place(x=320,y=170)
root.mainloop()
This code works pretty well enough till the place where pausing/playing/stopping buttons. Whenever I Try to press those button I get a ValueError: name 'media' is not defined. this is the link of the image of the error I get.The Problem I think is because I'm trying to use something which is not under its indent(media).
This is the player when it is working
I also tried other ways of returning this media attribute but they don't seem to work for me. So, It would be good if someone could help me with this error and fix it.Also,I'm welcome to any suggestions you have for the program.
Thanks
Sreeram
Upvotes: 0
Views: 140
Reputation: 46688
You can create media
outside search()
and use media.set_mrl()
inside search()
instead:
media = vlc.MediaPlayer()
def search():
...
best = video.getbest()
media.set_mrl(best.url)
...
Upvotes: 0
Reputation:
In the function, make media
global variable. However using global
is bad. You can go with OOP approach where you can access the instance variable anywhere inside the function provided it is defined
global media
media = vlc.MediaPlayer(best.url)
Upvotes: 1
Reputation: 406
I don't know that will work or not but try to pass the media value in the def like:
def play(media):
media.play()
Upvotes: 0