Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (45.3k points)

I'm brand new here and I'm trying to make a simple program to play shuffle music in python.

I've tried using pygame.mixer.music and list variables to pick a random song and play it. keep in mind I'm brand new and have no idea what I'm doing.

import pygame 

from pygame.locals import *

import random

import pygame.mixer

L = ['Relax.mp3', 'Wanchu_Back.mp3', 'Some_Chords.mp3', 'Green_Gusher.mp3',]

S = random.randint(0, len(L))

pygame.mixer.init()

pygame.mixer.music.set_volume(0.50)

pygame.mixer.music.load(L)

pygame.mixer.music.play(S)

I tried multiple things but I keep getting this message

pygame.error: Couldn't read from RWops

1 Answer

0 votes
by (16.8k points)

pygame.mixer.music.load() expects single filename or single file-object, not list.

pygame.mixer.music.load( filename )

ie.

pygame.mixer.music.load( L[0] )

If you need some random filename, then you use:

filename = random.choice(L)

pygame.mixer.music.load( filename )

To make sure it is good to use full path to file.

Refer here: https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.load

Related questions

0 votes
1 answer
asked Nov 25, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Nov 25, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Nov 25, 2019 in Python by Sammy (47.6k points)
...