[英]Can I pass a list to PyGame Music?
Win 10/64bit/3.5: 赢得10 / 64bit / 3.5:
I want to pass a list of .mp3s from within a variable ( mp3Fin ) to pygame.mixer.music.load() . 我想将一个变量中的.mp3列表( mp3Fin )传递给pygame.mixer.music.load() 。 Is there a way to do this? 有没有办法做到这一点? PyGame does work when I load separately. 当我分别加载时,PyGame确实可以工作。 I've only been learning programming/python for the last couple weeks and am new to stackoverflow, so sorry if I left anything out. 在过去的几周里,我一直只是在学习编程/ python,并且是stackoverflow的新手,所以如果遗漏任何内容,请对不起。
Last Edit Here is the latest code as it should be, properly formatted/indented. 最后编辑这是应该正确格式化或缩进的最新代码。 It is still getting an error, but the error has changed to something related to 'Rwops'. 它仍然出现错误,但错误已更改为与“ Rwops”相关的内容。 Only found a few links off bing about Rwops. 只发现了一些有关Rwops的链接。 Will delete the previous code because I don't think they're helpful anymore. 将删除先前的代码,因为我认为它们不再有用。
Code: 码:
if catChow == cat1[0]:
print("Welcome to SoCha's music player.")
def get_files(directories):
mp3Pre = []
for root, directories, files in os.walk(directories):
for filename in files:
#Joins the two strings in order to form the full filepath.
filepath = os.path.join(root, filename)
mp3Pre.append(filepath) #Adds to the mp3PreParse variable above.
return mp3Pre
mp3Post = get_files("C:\\Users\\SoCha\\Music")
pygame.mixer.init()
pygame.mixer.music.load([s for s in mp3Post if s.endswith(".mp3")])
pygame.mixer.music.play()
Rwops Error: Rwops错误:
Welcome to SoCha's music player.
Traceback (most recent call last):
File "SoChaOS.2.0.py", line 38, in <module>
pygame.mixer.music.load([s for s in mp3Post if s.endswith(".mp3")])
pygame.error: Couldn't read from RWops
***Repl Closed***
This is the last edit I'm making on this original post. 这是我对此原始帖子所做的最后修改。
mp3fin
is a string. mp3fin
是一个字符串。 Why are you indexing it? 为什么要索引它? When you call split()
it returns an array, which you print, and then throw away. 当您调用split()
它返回一个要打印的数组,然后将其丢弃。
In the last line pygame.mixer.music.load(mp3Fin[1])
you're splicing the string and taking the second character, which is a ':'. 在最后一行pygame.mixer.music.load(mp3Fin[1])
您要拼接字符串并采用第二个字符,即“:”。
You should probably just call pygame.mixer.music.load(mp3Fin)
您可能应该只调用pygame.mixer.music.load(mp3Fin)
I want to pass a list of .mp3s from within a variable (mp3Fin) to pygame.mixer.music.load(). 我想从变量(mp3Fin)中将.mp3列表传递给pygame.mixer.music.load()。
mp3Fin
isn't a List. mp3Fin
不是一个列表。 mp3Post is the list of files found in your directory. mp3Post是在目录中找到的文件列表。 Each individual file is in mp3Fin
as the for loop iterates. for循环迭代时,每个文件都在mp3Fin
中。 Try: 尝试:
for mp3Fin in mp3Post:
if mp3Fin.endswith(".mp3"):
pygame.mixer.init()
pygame.mixer.music.load(mp3Fin)
You can try passing the list to load
buy using the following: 您可以尝试使用以下方法传递列表以load
购买:
mp3Post = get_files("C:\\Users\\SoCha\\Music")
pygame.mixer.init()
pygame.mixer.music.load([s for s in mp3Post if s.endswith(".mp3")])
Remember that mp3Fin.split()
does not modify mp3Fin
in place - mp3Fin
stays a string! 请记住, mp3Fin.split()
不会就位修改mp3Fin
- mp3Fin
保持字符串!
So when you get to line 41, mp3Fin[1]
is getting the character at position 1 of mp3Fin
. 因此,当您到达第41 mp3Fin[1]
, mp3Fin[1]
将字符放在mp3Fin
位置1 mp3Fin
。 That's where the ":" in the error comes from, C:/...
. 这就是错误中的“:”来自C:/...
。
Try sending it the string directly 尝试直接发送字符串
pygame.mixer.music.load(mp3Fin)
Gotta be careful with those functions! 必须小心使用这些功能!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.