Python Music Player | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python Music Player

I'm trying to make a music player in Python, but every time I run the program nothing plays. Can someone please tell me what I'm doing wrong? EDIT: I'm running this in VS Code https://code.sololearn.com/cg0zPq6GECPT/?ref=app

30th Sep 2019, 11:24 PM
Code Babylon
Code Babylon - avatar
10 Answers
+ 2
‎ﹺ          ﹺ‎ he said he runned this on VS Code not sololearn, you should read the questions first. i used to play with pygame back in the days, let me open my dusty old files again.
1st Oct 2019, 3:13 PM
Shen Bapiro
Shen Bapiro - avatar
+ 2
so i've checked out my old music projects and the problem with your code is on the file path. i don't know what OS you're using if you're using windows the file path should use a backslash(\) however on mac and linux it's (/) and python basically reads slashes as an escape character, not as a character so if you try to print('\n') the slash and n wouldn't get printed and you get a newline instead. to overcome this you should change '\' into '\\' to cancel the escape character. so copy pasting the file path wouldn't make python able to read your files. you need to cancel out the escape characters first. so the buggy code should instead be mixer.music.load('C://Users//John Boland//Desktop//Code//VS Code//Python//Test_Codes//Fódlan Winds - FE Three Houses.mp3') i personally do not use this method since this is not an elegant way of using file paths if you're interested to use a better way of doing file paths then i recommend using the os.path.join() method
1st Oct 2019, 4:09 PM
Shen Bapiro
Shen Bapiro - avatar
+ 1
Code Babylon yes, you can also copy the music file path and paste it on your program to instantly get the file path.
1st Oct 2019, 5:58 PM
Shen Bapiro
Shen Bapiro - avatar
+ 1
lvl 1 crook I tried both \\ and // just now and neither played the audio.
2nd Oct 2019, 11:53 PM
Code Babylon
Code Babylon - avatar
+ 1
class MusicPlayer { private var songs: Array<String> = arrayOf() //your code goes here fun add(song:String):Array<String> { songs += song return songs } fun show() { for (song in songs) { println(song) } } fun play() { println ("Playing " + songs[0]) } } fun main(args: Array<String>) { val m = MusicPlayer() while(true) { var input = readLine()!!.toString() if(input == "stop") { break } m.add(input) } m.show() m.play() }
14th Sep 2021, 8:01 AM
Brigido bergado jr
Brigido bergado jr - avatar
0
lvl 1 crook I'm running this on Windows, so I use "\" correct?
1st Oct 2019, 5:49 PM
Code Babylon
Code Babylon - avatar
0
lvl 1 crook Just to clarify, I double \\?
1st Oct 2019, 6:37 PM
Code Babylon
Code Babylon - avatar
0
why don't you try them and see how it does
1st Oct 2019, 7:03 PM
Shen Bapiro
Shen Bapiro - avatar
0
pygame doesn’t work for python sololearn console
21st Jan 2021, 6:45 PM
Shreyaansh
Shreyaansh - avatar
0
Copy this answer and check correct answer and like thank you
14th Sep 2021, 8:02 AM
Brigido bergado jr
Brigido bergado jr - avatar