pythonaudiopygamemixer

NoneType object has no attribute 'play'


import pygame as pg, sys
from pygame.locals import *
import os

pg.mixer.pre_init(44100, 16, 2, 4096) 
pg.init()

a = pg.mixer.music.load("./Sounds/ChessDrop2.wav")
a.play()

The code above is what I have written to test whether sound can be played through pygame. My 'ChessDrop2.wav' file is a 16 bit wav-PCM file because when the file was 32 bit PCM, pygame recognised it as an unknown format. Now that error is gone when I run the code but the error below pops up on my shell instead. I have assigned the sound file to the variable 'a' so shouldn't the sound play? My version of python is 3.6.1 and pygame is 1.9.3.

a.play()
AttributeError: 'NoneType' object has no attribute 'play'

Solution

  • this functions doesn't return any object to be used, check the documentation:

    https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.load

    after loading the file you should use

    pg.mixer.music.play()