M
M
matveyvarg2015-07-29 09:37:08
Python
matveyvarg, 2015-07-29 09:37:08

Why doesn't pygame play mp3 without display?

import pygame
pygame.init()
pygame.display.set_mode((100,200))
pygame.mixer.music.load("first.mp3")
pygame.mixer.music.play(0)

if you remove the display.set_mode line, the script works, but the music does not play. Is there any way to play mp3 without a window? Well, or create an invisible window.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TomasHuk, 2015-07-29
@matveyvarg

I once used this code (sound/sound.py file):

import pygame, sys
def play(song):
    pygame.mixer.music.load(song)
    pygame.mixer.music.play()
    while pygame.mixer.music.get_busy():
            pos = pygame.mixer.music.get_pos()/ 1000

if __name__=='__main__':
    if len(sys.argv) > 1:
        song = sys.argv[1]
    else:
        song = 'file.mp3'

    pygame.mixer.init(22050, -16, 2, 2048)
    pygame.mixer.music.set_volume(2.0)
    play(song)
    pygame.quit()

Called from another script (file play_song.py):
def run(song):
    import os, sys
    pypath = sys.executable
    os.spawnv(os.P_DETACH, pypath, ('python', 'sound/sound.py', song))

if __name__=='__main__':
    song = 'sound/file.mp3'
    run(song)

Starting with one line from the third script:
import play_song
play_song.run('sound/first-file.mp3')

Plays mp3 without a window, as a separate process.

B
BarbosNikitos, 2018-11-08
@BarbosNikitos

from pygame import *
init()
from pygame import mixer
mixer.init()
song = mixer.music.load('Music2.mp3')
clock = time.Clock()
mixer.music.play(-1)
while True:
clock .tick(60)
quit() The
trick is that mp3 is loaded with load and wav and others with Sound

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question