Search code examples
pythonpython-3.xaudiopygamepython-multiprocessing

How can I play two songs at once using Pygame.mixer.music?


I have to make a player that can play two songs simultaneously. The problem is that the only module that I could find that supports every sound manipulation method that I need to use is Pygame.mixer.music. Unfortunately it only supports a single music stream at once.

I tried to cheat the system using threads and multiprocessing but it didn't do the job.

My question is does anybody know a Python3 module that can play 2 songs at once and has the following possibilities: pause, stop, seek through the song, change volume and change speed of the song. Or does anybody know how to do this with the Pygame module.

The multiprocessing code that I tried to use is down below if anybody can help with this I'd be grateful!

import pygame
import multiprocessing

pygame.mixer.init()


def play(arg):
    pygame.mixer.init()
    if arg:
        print(arg)
        pygame.mixer.music.load('song1.ogg')
        pygame.mixer.music.play()
    else:
        print(arg)
        pygame.mixer.music.load('song2.ogg')
        pygame.mixer.music.play()


p1 = multiprocessing.Process(target=play, args=(True,))
p2 = multiprocessing.Process(target=play, args=(False,))
p1.start()
p2.start()

while True:
    pass

Solution

  • I found a way to get the job done. At the moment I am using 2 sound manipulation modules:

    • Pygame.mixer.music
    • python-vlc

    They are working properly together and have the functions that I needed.