1

I'm trying to create three sound channels for my game.

music = pygame.mixer.Channel(0) 
ambience = pygame.mixer.Channel(1)
sfx = pygame.mixer.Channel(2)

The first two channels work perfectly. However, when calling the third one I get the error message:

AttributeError: 'dict' object has no attribute 'play'

I don't know why my 3rd channel is a dict instead of a channel object. Changing the amount of channels or order doesn't help.

This is my sound module.

import pygame
import os

pygame.mixer.init()
music = pygame.mixer.Channel(0) 
ambience = pygame.mixer.Channel(1)
sfx = pygame.mixer.Channel(2)

def get_sound(folder, sound):
    return pygame.mixer.Sound(os.path.join(os.path.dirname(__file__)+'\sounds',folder, sound))
    

sounds = {
    'ambient_sailing': get_sound('ambient', 'ambience_sailing_06.mp3'),
    'ambience_forest': get_sound('ambient', 'ambience_forest.mp3'),
    'ambience_dwarf_stronghold': get_sound('ambient', 'ambience_dwarf_stronghold.mp3'),
    'ambience_gnome_village': get_sound('ambient', 'ambience_gnome_village.mp3'),
    'happy_vikings' : get_sound('music', 'happy_vikings.wav'),
    'drifting' : get_sound('music', 'drifting.mp3'),

    }

sfx = {
    'ship_move' : get_sound('effects', 'ship_move_01.wav'),
    'select_destination' : get_sound('effects', 'select_destination_01.wav'),
    }
Joel S
  • 43
  • 5
  • From the [documentation](https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Channel) I see `The id must be a value from 0 to the value of pygame.mixer.get_num_channels().`. have you verified that? Also it says `Using channels is entirely optional since pygame can manage them by default.`. – Random Davis Dec 01 '21 at 20:31
  • Yes, as I stated changing the number of channels doesn't help. I need control over what and when the channels are playing so automatically assigning channels doesn't work. – Joel S Dec 01 '21 at 21:19
  • what does `pygame.mixer.get_num_channels()` return? – Michael West Jun 10 '23 at 16:04
  • I have this same problem and pygame.mixer.get_num_channels() returns 8. Is more than 2 channels actually supported? It says so in the documentation but it doesn't work. – Shariq Mobin Jul 20 '23 at 22:30

0 Answers0