简体   繁体   中英

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

You don't need any threading or multiprocessing. You can just paly 2 songs parallel using the pygame.mixer.Sound objects:

import pygame

pygame.mixer.init()
song_1 = pygame.mixer.Sound('song1.ogg')
song_2 = pygame.mixer.Sound('song2.ogg')
song_1.play(0)
song_2.play(0)

while pygame.mixer.get_busy():
    pygame.time.delay(10)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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