繁体   English   中英

Pygame使用pygame.mixer.music.load(file)播放声音会导致NoneType错误

[英]Pygame playing sound with pygame.mixer.music.load(file) gives NoneType error

我已经尝试了所有方法,但似乎无法播放文件

from pygame.locals import *
import pygame, codecs, os
class player(object):
    def __init__(self):
        self.get = dict()
        execfile('audio.conf', self.get)
        self.pid = os.getpid()
        pygame.init()
        self.currentlyBusy=0
        self.songs = list()
        self.nextSong = None
        self.soundInst=None
        self.sLib = None
        self.count = 0
        self.SONG_END = pygame.USEREVENT + 1
    def startPlaying(self, loop=False):
        if self.get['currentSong'] != None:
           if self.currentlyBusy != 1:
               self.play()
               self.currentlyBusy = 1
               else:
                 if self.currentlyBusy == 1 and loop == False:
                 self.stopPlaying()
                 self.play()
    def continueNextSong(self):
        with codecs.open('playlist.db', 'r', 'utf-8-sig') as f:
            for line in f.readlines():
                self.songs.append(line.strip())
                if len(self.songs) == self.count:
                    self.count = 0
                    self.nextSong = self.songs[self.count]
                else:
                    self.nextSong = self.songs[self.count]
    def play(self):
        if len(self.songs) > 0: pass

        else:
           with codecs.open('playlist.db', 'r', 'utf-8-sig') as f:
                           for line in f.readlines():
                                   self.songs.append(line.strip())
                       self.currentlyBusy = 1
                       path=self.songs[self.count]
                       self.sLib = {}
                       sound = self.sLib.get(path)
                       if sound == None:
                         cpath = path.replace('/', os.sep).replace('\\', os.sep)
                         sound = pygame.mixer.music.load(str(cpath))
                         print cpath
                         self.sLib[path] = sound
                         self.soundInst = self.sLib[path]
                         sound.play()
    def stopPlaying(self):
         self.soundInst.stop()
         self.currentlyBusy = 0
    def __START_PLAYER_SERVICE__(self):
         pygame.mixer.music.set_endevent(self.SONG_END)
         self.startPlaying(True)
         while True:
            for evt in pygame.event.get():
                if evt.type == self.SONG_END:
                    if self.get['loop'] == True:
                        self.playLastSong()
                     else:
                        if self.get['ploop'] == True:
                            self.count +=1
                            self.continueNextSong()

    def reload(self):
        execfile('audio.conf', self.get)
    def update(self):
            with codecs.open(self.config, 'wb', "utf-8-sig") as f:
                        song = self.get['currentSong']
                        w= '''
# LOOP THE SONG
loop=%s
# THIS IS JUST A TEXT FILE WITH FILE PATHS OF SONGS
playlist='%s'
# GETS MUSIC FROM THIS DIRECTORY
setMusicDir='%s'
# LOOP THE PLAYLIST
ploop=%s
# CURRENT SONG THE GUI WILL UPDATE THIS
currentSong='%s'
# THIS IS FOR THE VOLUME COMMANDLINE TOOL
# IF YOU DO NOT HAVE "gnome-alsa-mixer" AND "python-alsaaudio" DO NOT ACTIVATE
aSound=%s
# THIS IS A PYTHON SCRIPT THAT CONTROLS THE AUDIO VIA TERMINAL
volumeCommandTool='%s'
# THIS WILL JUST CONTROL VOLUME VIA GUI
volumeGuiControl='%s'
guiPID=%i
playerPID=%i
''' % (self.get['loop'], self.get['playlist'], self.get['setMusicDir'], self.get['ploop'], song, self.get['aSound'], self.get['volumeCommandTool'], self.get['volumeGuiControl'], self.get['guiPID'], self.pid)
                       f.write(u""+w)
                       f.close()
           self.reload()    
if __name__=="__main__":
    self = player()
    self.__START_PLAYER_SERVICE__()

当脚本运行时,如果config.conf中有currentSong的值,则它将播放;如果没有值,则self.songs中的第一个文件将播放。 但是当我打电话时

pygame.mixer.music.load(self.song[self.count])

我收到一个错误消息,

Traceback (most recent call last):
  File "player.py", line 100, in <module>
    self.__START_PLAYER_SERVICE__()
  File "player.py", line 58, in __START_PLAYER_SERVICE__
    self.startPlaying(True)
  File "player.py", line 19, in startPlaying
    self.play()
  File "player.py", line 52, in play
    sound.play()
AttributeError: 'NoneType' object has no attribute 'play'

但我不知道该如何解决。 我正在运行Ubuntu 12.04 LTS 64AMD

我可能是错的,目前无法测试,但请尝试:

pygame.mixer.music.load(str(cpath))
pygame.mixer.music.play()

pygame.mixer.music.load()返回无

pygame.mixer.music.load(str(cpath))

它不返回声音对象。 您可以说pygame.mixer.music.play(),它将播放当前加载的音乐。

要么:

sound = pygame.mixer.Sound(str(cpath))
channel = sound.play()

可以加载多种声音,并且可以与通道对象一起播放。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM