【问题标题】:How can I play two songs at once using Pygame.mixer.music?如何使用 Pygame.mixer.music 一次播放两首歌曲?
【发布时间】:2018-12-16 19:51:06
【问题描述】:

我必须制作一个可以同时播放两首歌曲的播放器。 问题是我能找到的唯一支持我需要使用的每种声音处理方法的模块是 Pygame.mixer.music。不幸的是,它一次只支持一个音乐流。

我尝试使用线程和多处理来欺骗系统,但没有成功。

我的问题是有没有人知道一个 Python3 模块,它可以一次播放 2 首歌曲,并且有以下可能性:暂停、停止、搜索歌曲、改变音量和改变歌曲的速度。 或者有人知道如何使用 Pygame 模块做到这一点。

我尝试使用的多处理代码如下所示,如果有人能提供帮助,我将不胜感激!

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

【问题讨论】:

    标签: python python-3.x audio pygame python-multiprocessing


    【解决方案1】:

    您不需要任何线程或多处理。您可以使用 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)
    

    【讨论】:

    • pygame.mixer.Sound 对象没有我应该做的事情所必需的暂停和搜索选项。
    • @aon2003 pygam.mixer 模块有一个pygame.mixer.pause 方法。没有 pygame 模块有搜索功能。所以你的问题没有解决办法。很抱歉我试图帮助你。
    【解决方案2】:

    我找到了完成工作的方法。 目前我正在使用 2 个声音处理模块:

    • Pygame.mixer.music
    • python-vlc

    它们一起正常工作并且具有我需要的功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      相关资源
      最近更新 更多