Error when loading audio file from zip in python

不羁的心 提交于 2019-12-24 04:08:13

问题


I am making a game, and I need to load some password protected audio files from a .zip file, but I get this error:

io.UnsupportedOperation: seek
io.UnsupportedOperation: seek
io.UnsupportedOperation: seek
b'hey you did it!' #THIS IS FROM THE PROGRAM
Traceback (most recent call last):
  File "C:\Python36\lib\zipfile.py", line 849, in read
    data = self._read1(n)
  File "C:\Python36\lib\zipfile.py", line 917, in _read1
    data += self._read2(n - len(data))
  File "C:\Python36\lib\zipfile.py", line 949, in _read2
    data = self._fileobj.read(n)
  File "C:\Python36\lib\zipfile.py", line 705, in read
    self._file.seek(self._pos)
AttributeError: 'NoneType' object has no attribute 'seek'

And this is my code below:

from zipfile import ZipFile
from PIL import Image
from io import BytesIO
import pygame
from pygame.locals import *
import pyganim
import sys

pygame.init()
root = pygame.display.set_mode((320, 240), 0, 32)
pygame.display.set_caption('image load test')


#THIS IS HOW TO LOAD IMAGES (WORKS)
with ZipFile("spam.zip", 'r') as archive:
    mcimg = archive.read('a.png', pwd=b'onlyforthedev')
    mc = pygame.image.load(BytesIO(mcimg))
    anime = pyganim.PygAnimation([(mc, 100),
                                  (mc, 100)])
    anime.play()

#THIS IS HOW TO LOAD MUSIC (DOES NOT WORK)
with ZipFile('spam.zip') as zippie:
    with zippie.open('zora.mp3', pwd=b'onlyforthedev') as zora:
        pygame.mixer.music.load(zora)
        pygame.mixer.music.play(-1)

#THIS IS HOW TO LOAD TEXT (WORKS)
with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt', pwd=b'onlyforthedev') as myfile:
        print(myfile.read())

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    root.fill((100, 50, 50))
    anime.blit(root, (100, 50))
    pygame.display.update()

What can I do to load sound files without raising such an error? And what is 'seek'?


回答1:


I also get this error on python 3.6.

I am going to guess that pygame.mixer.music.load calls the seek method on zippie, which is a ZipExtFile.

From python 3.7 ZipExtFile objects now have a seek method. I think that if you upgrade to python 3.7.2 or newer, then your error should go away.




回答2:


Try to replace

pygame.mixer.music.load(zora)

with

with BytesIO(zora.read()) as zora_bio:
    pygame.mixer.music.load(zora_bio)

This worked for me on python 3.6 with h5py.File().
I'm guessing it's the same problem as with pygame..load().


EDIT:
I now realize the above solution already exists in your code when you LOAD IMAGES:

with ZipFile("spam.zip", 'r') as archive:
    mcimg = archive.read('a.png', pwd=b'onlyforthedev')
    mc = pygame.image.load(BytesIO(mcimg))

So for uniformity, you could LOAD MUSIC in a similar way:

with ZipFile('spam.zip') as zippie:
    zora = zippie.read('zora.mp3', pwd=b'onlyforthedev')
    pygame.mixer.music.load(BytesIO(zora))


来源:https://stackoverflow.com/questions/52311026/error-when-loading-audio-file-from-zip-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!