Chris Lavan
Chris Lavan

Reputation: 23

Play m4a in python script with mplayer

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.

Upvotes: 1

Views: 7419

Answers (2)

Isa Hassen
Isa Hassen

Reputation: 300

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)

Upvotes: 0

Deerenaros
Deerenaros

Reputation: 30

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()

Upvotes: 1

Related Questions