Re: chat on some programming languages

Something to keep in mind is that I ran into problems with case sensative files at times, so it might not hurt to double check the capitalization of your file extentions and names. Now, assuming your using the latest Pyinstaller lets say you want to pack a zip file containing your music and want to load it at run time, you start by adding this function to your py script:

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)

    return os.path.join(os.path.abspath("."), relative_path)

This finds the absolute path to the temporary folder created at run time when running the executable, and where the zip file should end up. Next we load and unpack the audio files, in this example i'm using one of my own OpenAL functions to load the sound into self.sound, if your using a different method you may need to use resource_path(fileName) to link to the file.

import zipfile
import os
import sys

myzipfile = zipfile.ZipFile(resource_path('data.zip'),'r')

#if you have a password on the zip, this line sets the zipfile password for reading, otherwise exclude.
#myzipfile.setpassword('password')

for fileName in myzipfile.namelist():
    if fileName == 'sound.wav':
        myzipfile.extract(fileName,resource_path(''))
        self.sound = load_sound(fileName)
        os.remove(resource_path(fileName))

After that we run Pyinstaller.py to generate a spec file and apply the tag to pack it as a single file:

pyinstaller.py --onefile --noconsole yourprogram.py

Then we edit the spec file and put in the line to include our zip file between the "pyz = PYZ" and "exe = EXE" lines:

a.datas += [('data.zip','C:\folder\\data.zip', 'DATA')]

Then we recompile the spec file:

pyinstaller.py yourprogram.spec

This will grab and include the zipfile into the final executable and unpack it into the temporary folder created when the program is run. There are a few extra steps you can take to protect the zip by compiling your main script as a PYC binary and using a separate loader script, or running it through an obfusticator to make the script less readable in case anyone tries to extract it and lookup the password.


Edit: If anyones curious here's the OpenAL loader class i'm using, which unfortunately only works with wav files:

import wave
import os
import sys

class load_sound(object):
    def __init__(self,filename):
        self.name = filename
    #load/set audio file
        if len (sys.argv) < 2:
            print ("Usage: %s wavefile" % os.path.basename(sys.argv[0]))
            print ("    Using an example wav file...")
            dirname = os.path.dirname(os.path.realpath(__file__))
##            dirname += '/data/'
            fname = os.path.join(dirname, filename)
        else:
            fname = sys.argv[1]

        wavefp = wave.open(fname)
        channels = wavefp.getnchannels()
        bitrate = wavefp.getsampwidth() * 8
        samplerate = wavefp.getframerate()
        wavbuf = wavefp.readframes(wavefp.getnframes())
        self.duration = (len(wavbuf) / float(samplerate))/2
        self.length = len(wavbuf)
        formatmap = {
            (1, 8) : al.AL_FORMAT_MONO8,
            (2, 8) : al.AL_FORMAT_STEREO8,
            (1, 16): al.AL_FORMAT_MONO16,
            (2, 16) : al.AL_FORMAT_STEREO16,
        }
        alformat = formatmap[(channels, bitrate)]

        self.buf = al.ALuint(0)
        al.alGenBuffers(1, self.buf)
    #allocate buffer space to: buffer, format, data, len(data), and samplerate
        al.alBufferData(self.buf, alformat, wavbuf, len(wavbuf), samplerate)

    def delete(self):
        al.alDeleteBuffers(1, self.buf)
_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : danny via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : helsywarner via Audiogames-reflector

Reply via email to