Play m4a in python script with mplayer

六眼飞鱼酱① 提交于 2019-12-13 19:50:35

问题


I'm posting to a URL, downloading an audio file (m4a) and trying to play it from the terminal with a Python script. When I type

mplayer asdf.m4a 

in the terminal it plays fine. But when I execute the following code

from mplayer import Player

player = Player()
player.loadfile('asdf.m4a')

as shown in the mplayer guide, I get the following errors:

mplayer: could not connect to socket
mplayer: No such file or directory

I've been trying to figure this out for a couple days now and it seems like it should be real simple. I don't know what's wrong. I was able to use pygame to play mp3's and ogg's but I need to play m4a and I just can't seem to get mplayer to work for me.

The only related issues I've seen suggested adding nolirc=yes to the mplayer config file. Didn't help.

Any help would be greatly appreciated.


回答1:


Worst way, but could be usefull:

from subprocess import Popen, PIPE

pipes = dict(stdin=PIPE, stdout=PIPE, stderr=PIPE)
mplayer = Popen(["mplayer", "asdf.m4a"], **pipes)

# to control u can use Popen.communicate
mplayer.communicate(input=b">")
sys.stdout.flush()



回答2:


Try using the absolute path to the file. If you are running this script in an IDE or debugger, sometimes it can mess up the relative path.

I would try:

import os
from mplayer import Player

player = Player()
abspath = os.path.join(os.path.dirname(__file__), 'asdf.m4a')
player.loadfile(abspath)


来源:https://stackoverflow.com/questions/28664813/play-m4a-in-python-script-with-mplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!