Python Forum
what am i doing wrong!? music wont play
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what am i doing wrong!? music wont play
#1
I am using a raspberry pi and my script is not complete but the shuffle music button is not working i don't know what I did wrong
I am using pygame for media and wxpython for GUI. please someone help
my code is here please note that this is my firstr actual program and thread on the forums
#!/usr/bin/python3
#import needed libraries
import wx
import pygame
import multiprocessing
import os
import json
import filetype
import serial
import urllib
import random
import time
#init pygame.mixer for media player
pygame.init()
pygame.mixer.init()
#paths definition
proot = os.getcwd()
music = os.path.join(os.environ["HOME"], "Music")
conf = os.path.join(proot, "config_files")
fmain_c = os.path.join(conf, "conf.json")
usb_drive_locs = "/media/{}/".format(os.environ["USER"])

#gmaps url structure
gmaps_url_start = "https://maps.googleapis.com/maps/api/directions/json?origin="
gmaps_url_mid = "&destination="
gmaps_url_end = "&key="

#main class for program
class mw(wx.Frame):
    #init method
    def __init__(self, parent, id=-1, settings={"TIRE_SIZE":26.0,"GPSKEY":None,"SERIAL_PORT":"dev/Serial0","MUSIC_LOCATIONS":["path/to/music"],"WINDOW_SIZE":[1000,800]}):
        #Pull variables from global
        self.settings = settings
        #define var for all serial data on the serial port
        self.serData = {"BATV":"","BATP":"","BATS":"","GPS_COORD_LON":"","GPS_COORD_LAT":"","GPS_COORD_ALT":"","RPM":"","RPM_SPD":"","TEMP":"","HUMID":""}
        self.transThread = {"MUSIC":{"PLAY":False,"PAUSE":False,"SKIP":False,"PREV":False,"PAST":[None]},"DIR":{"NEXT":False,"LAST":False,"DIRLIST":None,"DIRSTEP":None}}
        #start the gui
        wx.Frame.__init__(self,parent,id,"BikePc",size=self.settings["WINDOW_SIZE"])
        panel =  wx.Panel(self)
        #buttons and io start
        self.Bind(wx.EVT_CLOSE, self.closeAppX)
        quitBtn = wx.Button(panel, label="Quit App", pos=[10,10], size=[70,30])
        dirBtn = wx.Button(panel, label="GMaps", pos=[90,10], size=[60,30])
        musBtn = wx.Button(panel, label="Shuffle Music", pos=[160,10], size=[105,30])
        prevBtn = wx.Button(panel, label="Previous Song", pos=[275,10], size=[110,30])
        playBtn = wx.Button(panel, label="Play", pos=[395,10], size=[40,30])
        pauseBtn = wx.Button(panel, label="Pause", pos=[445,10], size=[45,30])
        skipBtn = wx.Button(panel, label="Skip song", pos=[500,10], size=[75,30])
        lastBtn = wx.Button(panel, label="GMaps Last Instruction", pos=[585,10], size=[170,30])
        nextBtn = wx.Button(panel, label="GMaps Next Instruction", pos=[765,10], size=[170,30])
        self.Bind(wx.EVT_BUTTON, self.dirStart, dirBtn)
        self.Bind(wx.EVT_BUTTON, self.startMusicThread, musBtn)
        self.Bind(wx.EVT_BUTTON, self.prevSong, prevBtn)
        self.Bind(wx.EVT_BUTTON, self.pauseSong, pauseBtn)
        self.Bind(wx.EVT_BUTTON, self.playSong, playBtn)
        self.Bind(wx.EVT_BUTTON, self.nextGps, nextBtn)
        self.Bind(wx.EVT_BUTTON, self.lastGps, lastBtn)
        self.Bind(wx.EVT_BUTTON, self.closeAppButton, quitBtn)
    #exit by window close
    def closeAppX(self, event):
        self.Destroy()
    #close by button press
    def closeAppButton(self, event):
        self.Close(True)
    #direction with gps start new direction/ check if available to system
    def dirStart(self, event):
        #if there is no gmaps api key, say errored out and quit
        if(self.settings["GPSKEY"] == None):
            notifyBox = wx.MessageDialog(None, "error. set up for only gps coords","error", wx.OK)
            answer = notifyBox.Show()
            notifyBox.Destroy()
            return
        else:
            self.getGps(key=self.settings["GPSKEY"])
    #get user input for gps direction
    def getGps(self, key):
        pass
    #from btn press skip song
    def skipSong(self, event):
        self.transThread["MUSIC"].update({"SKIP":True})
    #from btn press go back a song
    def prevSong(self, event):
        self.transThread["MUSIC"].update({"PREV":True})
    #from btn press pause music
    def pauseSong(self, event):
        self.transThread["MUSIC"].update({"PAUSE":True})
    #from btn press play music
    def playSong(self, event):
        self.transThread["MUSIC"].update({"PAUSE":False})
    #from btn press get next gps direction
    def nextGps(self, event):
        if(not self.settings["GPSKEY"] == None or not self.transThread["DIR"]["DIRLIST"][0] == None):
            dirLen = len(self.transThread["DIR"]["DIRLIST"])
            for i in randge(0,dirLen):
                if(i == self.transThread["DIR"]["DIRSTEP"]):
                    self.transThread["DIR"].update({"DIRSTEP":i+1})
    #from btn press get last gps direction
    def lastGps(self, event):
        if(not self.settings["GPSKEY"] == None or not self.transThread["DIR"]["DIRLIST"][0] == None):
            dirLen = len(self.transThread["DIR"]["DIRLIST"])
            for i in randge(0,dirLen):
                if(i == self.transThread["DIR"]["DIRSTEP"]):
                    self.transThread["DIR"].update({"DIRSTEP":i-1})
    #process json for gps direction
    def gpsProcessData(self):
        pass
    #search for music
    def musicSearch(self):
        if self.settings["MUSIC_LOCATIONS"][0] == "path/to/music":
            pass
        else:
            for i in self.settings["MUSIC_LOCATIONS"]:
                for root, subdirs, files in os.walk(i):
                    for file in files:
                        path = os.path.join(root, file)
                        kind = filetype.guess(path)
                        if(kind is None):
                            pass
                        elif(kind.extension == "mp3" or kind.extension == "ogg"):
                            self.transThread["MUSIC"]["MUSICLIST"].append(path)
    #music play start
    def startMusicThread(self, event):
        self.transThread["MUSIC"].update({"PLAY":True,"PAUSE":False,"SKIP":False,"PREV":False,"PAST":[None]})
        self.musicSearch()
        musicThread = multiprocessing.Process(target=self.musThread, daemon=True)
        musicThread.start()
    #start a song
    def musSongStart(self, songName=None):
        if(not songName == None):
            songVal = random.randrange(0,self.music["LEN"])
            pygame.mixer.load(self.music["LIST"][songVal])
            self.transThread["MUSIC"]["PAST"].append(self.music["LIST"][songVal])
            pygame.mixer.play()
        
    #thread for music
    def musThread(self):
        while(True):
            if(self.transThread["MUSIC"]["PLAY"] == False):
                break
            else:
                while(True):
                    self.musSongStart()
                    while(pygame.mixer.get_busy()):
                        if(self.transThread["MUSIC"]["PAUSE"]):
                            while(self.transThread["MUSIC"]["PAUSE"]):
                                pass
                        elif(self.transThread["MUSIC"]["SKIP"]):
                            pygame.mixer.stop()
                            self.transThread["MUSIC"].update({"SKIP":False})
                        elif(self.transThread["MUSIC"]["PREV"]):
                            pygame.mixer.stop()
                            self.musSongStart(songName=self.transThread["MUSIC"]["PAST"][:1])
                            self.transThread["MUSIC"].update({"PREV":True})
                        elif(self.transThread["MUSIC"]["PLAY"] == False):
                            pygame.mixer.stop()
                            break
                    time.sleep(.01)
    #serial request
    def serialRequestData(self):
        pass

if(__name__ == "__main__"):
    app = wx.App()
    frame = mw(parent=None, settings={"TIRE_SIZE":26.0,"GPSKEY":None,"SERIAL_PORT":"dev/Serial0","MUSIC_LOCATIONS":["/media/pi/","/home/pi/Music"],"WINDOW_SIZE":[950,700]})
    frame.Show()
    app.MainLoop()
    pygame.mixer.quit()
    pygame.quit()
    quit()
thanks jusrostyler1123
Reply
#2
for proot you are using os.getcwd()

do you want the folder of the script as proot ?

try that and see if the results are different

import os
import sys

proot = os.getcwd()
proot2 = os.path.dirname(sys.argv[0])
print(proot)
print(proot2)
Quote:music = os.path.join(os.environ["HOME"], "Music")

gives

Output:
/home/username/Music
if you use localized folder names (in Linux) it will not work

I use in Linux to get localized user folders

from gi.repository import GLib

music = GLib.get_user_special_dir(GLib.USER_DIRECTORY_MUSIC)
print(music)
Reply
#3
yeah using a pi that's probably why ill check

still, nothing IDK what to do iv considered a new route for graphics and hope that using flask as GUI (lol I know) but I think having two graphics engines on the same script might be the problem still open to suggestions from this way though
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why wont this path work one way, but will the other way? cubangt 2 669 Sep-01-2023, 04:14 PM
Last Post: cubangt
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,539 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  How can I send a .mp3 to play through my mic? ejected 20 20,187 Sep-01-2022, 02:38 AM
Last Post: victormayer
  Play the next music in a list Pymax 0 1,204 Jul-28-2021, 07:27 PM
Last Post: Pymax
  how do i play an mp3 from a list gr3yali3n 3 2,131 Dec-01-2020, 08:50 AM
Last Post: Axel_Erfurt
  queued music wont play, how to do this? glennford49 0 1,385 Nov-24-2020, 12:54 PM
Last Post: glennford49
  When I import a Module it wont run PyNovice 17 6,390 Oct-26-2019, 11:14 AM
Last Post: PyNovice
  python gives wrong string length and wrong character thienson30 2 2,994 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  How to play against the computer Help_me_Please 4 4,062 Aug-29-2019, 03:37 PM
Last Post: ThomasL
  Why wont this work? ejected 2 3,186 Mar-29-2019, 05:33 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020