Oct-30-2019, 10:59 PM
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
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
