简体   繁体   English

如何使用 Pygame.mixer.music 一次播放两首歌曲?

[英]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.问题是我能找到的唯一支持我需要使用的每种声音处理方法的模块是 Pygame.mixer.music。 Unfortunately it only supports a single music stream at once.不幸的是,它一次只支持单曲 stream。

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.我的问题是有人知道一个 Python3 模块,它可以一次播放 2 首歌曲,并且具有以下可能性:暂停、停止、搜索歌曲、改变音量和改变歌曲的速度。 Or does anybody know how to do this with the Pygame module.或者有人知道如何使用 Pygame 模块来做到这一点。

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:您可以使用pygame.mixer.Sound对象并行播放 2 首歌曲:

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:目前我正在使用 2 个声音处理模块:

  • Pygame.mixer.music Pygame.mixer.music
  • python-vlc python-vlc

They are working properly together and have the functions that I needed.它们一起正常工作并具有我需要的功能。

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

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