python-mplayer无法打开mp3文件

1 投票
1 回答
808 浏览
提问于 2025-04-30 21:06

你好,我正在尝试制作一个小型音频播放器,把mplayer集成到Python中。

我以为python-mplayer可以完成这个任务,但我无法让它正常工作。有什么建议吗?

看起来p.loadfile这个命令不起作用,因为在执行ps ax时显示了以下内容:

/usr/bin/mplayer -slave -idle -really-quiet -msglevel global=4 -input nodefault-bindings -noconfig all

import pygame
import os
import subprocess
import sys
import time
import subprocess

from mplayer import *

audiofile = "/home/user/1.mp3"
gfx_rev_normal = pygame.image.load('rev_normal.png')

pygame.init()
screen = pygame.display.set_mode((800, 480))

#background = pygame.Surface(screen.get_size())
#background = background.convert()
#background.fill = ((255, 255, 255))

font = pygame.font.SysFont("monospace", 36)
source_text = font.render("Audio Player", 1, (255, 255, 255))
text_width = source_text.get_width()
source_text_x = screen.get_width() / 2
source_text_x = source_text_x - (text_width/2)
source_text_y = 10
screen.blit(source_text,(source_text_x, source_text_y))

Player.exec_path="/usr/bin/mplayer"
p = Player()
p.loadfile('audiofile')
#p.pause()
p.volume=100

running = True
while running:

    time.sleep(0.1)
    print p.stream_pos
    screen.blit(gfx_rev_normal,(30,120))
    pygame.display.flip()

    for event in pygame.event.get():

    if event.type == pygame.QUIT:
        running = False

    if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
        running = False
暂无标签

1 个回答

2

我对mplayer的API不太熟悉,不过可能只是这个小错误:

p.loadfile('audiofile')

应该改成

p.loadfile(audiofile)

因为你的文件路径在变量audiofile里,而不是字符串'audiofile'

撰写回答