How do I setup file name for mplayer

I am attaching some code that allows me to communicate with MpPlayer from Java.  Things work fine except I cannot play any mp3 files that have a space in the title.  I am embedding the code that I am using along with the error log output.  I have been messing around with lines 93 and 94 putting a backslash before any spaces in the command line that goes to MPlayer but nothing seems to help.  If I remove the spaces in the file name in the program and rename the file on the computer removing all spaces it works fine.  Anyone with any idea?

 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jmplayer2;

import java.io.*;
import java.util.logging.Logger;
import java.util.logging.Level;

/**
 * A player which is actually an interface to MPlayer.
 */
public class JMPlayer2 {

    private static Logger logger = Logger.getLogger(JMPlayer2.class.getName());

    /** A thread that reads from an input stream and outputs to another line by line. */
    private static class LineRedirecter extends Thread {
        /** The input stream to read from. */
        private InputStream in;
        /** The output stream to write to. */
        private OutputStream out;
        /** The prefix used to prefix the lines when outputting to the logger. */
        private String prefix;

        /**
         * @param in the input stream to read from.
         * @param out the output stream to write to.
         * @param prefix the prefix used to prefix the lines when outputting to the logger.
         */
        LineRedirecter(InputStream in, OutputStream out, String prefix) {
            this.in = in;
            this.out = out;
            this.prefix = prefix;
        }

        public void run()
        {
            try {
                // creates the decorating reader and writer
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                PrintStream printStream = new PrintStream(out);
                String line;

                // read line by line
                while ( (line = reader.readLine()) != null) {
                    logger.info((prefix != null ? prefix : "") + line);
                    printStream.println(line);
                }
            } catch (IOException exc) {
                logger.log(Level.WARNING, "An error has occured while grabbing lines", exc);
            }
        }

    }

    /** The path to the MPlayer executable. */
    private String mplayerPath = "/usr/bin/mplayer -slave -quiet ";
    /** Options passed to MPlayer. */
    private String mplayerOptions = "";

    /** The process corresponding to MPlayer. */
    private Process mplayerProcess;
    /** The standard input for MPlayer where you can send commands. */
    private PrintStream mplayerIn;
    /** A combined reader for the the standard output and error of MPlayer. Used to read MPlayer responses. */
    private BufferedReader mplayerOutErr;

    public JMPlayer2() {
    }

    /** @return the path to the MPlayer executable. */
    public String getMPlayerPath() {
        return mplayerPath;
    }

    /** Sets the path to the MPlayer executable.
     * @param mplayerPath the new MPlayer path; this will be actually efective
     * after {@link #close() closing} the currently running player.
     */
    public void setMPlayerPath(String mplayerPath) {
        this.mplayerPath = mplayerPath;
    }

    public void open(File file) throws IOException {

        String path = "/home/al/music/audio/" + file;
        path = path.replaceAll(" ", "\\\\ ");
        //String path = file.getAbsolutePath().replace('\\', '/');
        
        if (mplayerProcess == null) {
            // start MPlayer as an external process
            //String command = "\"" + mplayerPath + path + "\"";
            String command = mplayerPath + path;
            logger.info("Starting MPlayer process: " + command);
System.out.println("command = " + command);            
            mplayerProcess = Runtime.getRuntime().exec(command);

            // create the piped streams where to redirect the standard output and error of MPlayer
            // specify a bigger pipesize
            PipedInputStream  readFrom = new PipedInputStream(1024*1024);
            PipedOutputStream writeTo = new PipedOutputStream(readFrom);
            mplayerOutErr = new BufferedReader(new InputStreamReader(readFrom));

            // create the threads to redirect the standard output and error of MPlayer
            new LineRedirecter(mplayerProcess.getInputStream(), writeTo, "MPlayer says: ").start();
            new LineRedirecter(mplayerProcess.getErrorStream(), writeTo, "MPlayer encountered an error: ").start();

            // the standard input of MPlayer
            mplayerIn = new PrintStream(mplayerProcess.getOutputStream());
        } else {
            execute("loadfile \"" + path + "\" 0");
        }
        // wait to start playing
        waitForAnswer("Starting playback...");
        logger.info("Started playing file " + path);
    }

    public void close() {
        if (mplayerProcess != null) {
            execute("quit");
            try {
                mplayerProcess.waitFor();
            }
            catch (InterruptedException e) {}
            mplayerProcess = null;
        }
    }

    public File getPlayingFile() {
        String path = getProperty("path");
        return path == null ? null : new File(path);
    }

    public void togglePlay() {
        execute("pause");
    }

    public boolean isPlaying() {
        return mplayerProcess != null;
    }

    public long getTimePosition() {
        return getPropertyAsLong("time_pos");
    }

    public void setTimePosition(long seconds) {
        setProperty("time_pos", seconds);
    }

    public long getTotalTime() {
        return getPropertyAsLong("length");
    }

    public float getVolume() {
        return getPropertyAsFloat("volume");
    }

    public void setVolume(float volume) {
        setProperty("volume", volume);
    }

    protected String getProperty(String name) {
        if (name == null || mplayerProcess == null) {
            return null;
        }
        String s = "ANS_" + name + "=";
        String x = execute("get_property " + name, s);
        if (x == null)
            return null;
        if (!x.startsWith(s))
            return null;
        return x.substring(s.length());
    }

    protected long getPropertyAsLong(String name) {
        try {
            return Long.parseLong(getProperty(name));
        }
        catch (NumberFormatException exc) {}
        catch (NullPointerException exc) {}
        return 0;
    }

    protected float getPropertyAsFloat(String name) {
        try {
            return Float.parseFloat(getProperty(name));
        }
        catch (NumberFormatException exc) {}
        catch (NullPointerException exc) {}
        return 0f;
    }

    protected void setProperty(String name, String value) {
        execute("set_property " + name + " " + value);
    }

    protected void setProperty(String name, long value) {
        execute("set_property " + name + " " + value);
    }

    protected void setProperty(String name, float value) {
        execute("set_property " + name + " " + value);
    }

    /** Sends a command to MPlayer..
     * @param command the command to be sent
     */
    private void execute(String command) {
        execute(command, null);
    }

    /** Sends a command to MPlayer and waits for an answer.
     * @param command the command to be sent
     * @param expected the string with which has to start the line; if null don't wait for an answer
     * @return the MPlayer answer
     */
    private String execute(String command, String expected) {
        if (mplayerProcess != null) {
            logger.info("Send to MPlayer the command \"" + command + "\" and expecting "
                    + (expected != null ? "\"" + expected + "\"" : "no answer"));
            mplayerIn.print(command);
            mplayerIn.print("\n");
            mplayerIn.flush();
            logger.info("Command sent");
            if (expected != null) {
                String response = waitForAnswer(expected);
                logger.info("MPlayer command response: " + response);
                return response;
            }
        }
        return null;
    }

    /** Read from the MPlayer standard output and error a line that starts with the given parameter and return it.
     * @param expected the expected starting string for the line
     * @return the entire line from the standard output or error of MPlayer
     */
    private String waitForAnswer(String expected) {
        // todo add the possibility to specify more options to be specified
        // todo use regexp matching instead of the beginning of a string
        String line = null;
        if (expected != null) {
            try {
                while ((line = mplayerOutErr.readLine()) != null) {
                    logger.info("Reading line: " + line);
                    if (line.startsWith(expected)) {
                        return line;
                    }
                }
            }
            catch (IOException e) {
            }
        }
        return line;
    }

    public static void main(String[] args) throws IOException {
        JMPlayer2 jmPlayer = new JMPlayer2();
        // open a video file
        jmPlayer.open(new File("Slow Hand.mp3"));
        // skip 2 minutes
        //jmPlayer.setTimePosition(120);
        // set volume to 90%
        jmPlayer.setVolume(50);        
    }
}

Open in new window

run:
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2 open
command = /usr/bin/mplayer -slave -quiet /home/al/music/audio/Slow\ Hand.mp3
INFO: Starting MPlayer process: /usr/bin/mplayer -slave -quiet /home/al/music/audio/Slow\ Hand.mp3
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer says: MPlayer dev-SVN-r34016-4.5-openSUSE Linux 11.4 (i686)-Packman (C) 2000-2011 MPlayer Team
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer encountered an error: Can't open joystick device /dev/input/js0: No such file or directory
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer encountered an error: Can't init input joystick
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer encountered an error: mplayer: could not connect to socket
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer encountered an error: mplayer: No such file or directory
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer encountered an error: Failed to open LIRC support. You will not be able to use your remote control.
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer says: Loading extension-related profile 'vo.vdpau'
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer says: 
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer says: Playing /home/al/music/audio/Slow\.
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer encountered an error: File not found: '/home/al/music/audio/Slow\'
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer encountered an error: Failed to open /home/al/music/audio/Slow\.
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer says: 
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer says: Loading extension-related profile 'vo.vdpau'
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer says: 
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer says: Playing Hand.mp3.
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer encountered an error: File not found: 'Hand.mp3'
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer encountered an error: Failed to open Hand.mp3.
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer says: 
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer says: 
Jan 3, 2012 12:31:25 AM jmplayer2.JMPlayer2$LineRedirecter run
INFO: MPlayer says: Exiting... (End of file)
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: MPlayer dev-SVN-r34016-4.5-openSUSE Linux 11.4 (i686)-Packman (C) 2000-2011 MPlayer Team
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: Can't open joystick device /dev/input/js0: No such file or directory
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: Can't init input joystick
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: mplayer: could not connect to socket
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: mplayer: No such file or directory
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: Failed to open LIRC support. You will not be able to use your remote control.
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: Loading extension-related profile 'vo.vdpau'
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: 
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: Playing /home/al/music/audio/Slow\.
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: File not found: '/home/al/music/audio/Slow\'
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: Failed to open /home/al/music/audio/Slow\.
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: 
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: Loading extension-related profile 'vo.vdpau'
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: 
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: Playing Hand.mp3.
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: File not found: 'Hand.mp3'
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: Failed to open Hand.mp3.
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: 
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: 
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 waitForAnswer
INFO: Reading line: Exiting... (End of file)
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 open
INFO: Started playing file /home/al/music/audio/Slow\ Hand.mp3
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 execute
INFO: Send to MPlayer the command "set_property volume 50.0" and expecting no answer
Jan 3, 2012 12:31:26 AM jmplayer2.JMPlayer2 execute
INFO: Command sent
BUILD SUCCESSFUL (total time: 1 second)

Open in new window

sargent240Asked:
Who is Participating?
 
CEHJCommented:
fyi, the following worked fine for me:
public static void main(String[] args) {
        try {
            String[] MPLAYER_ARGS = {
                    "/usr/bin/mplayer", "-quiet", "/tmp/Slow Hand.mp3"
                };

            Process mplayerProcess = Runtime.getRuntime().exec(MPLAYER_ARGS);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

Open in new window

0
 
CEHJCommented:
First of all, try using the String[] form of Runtime.exec (not String) - it's much kinder in relation to the shell

If you're still having trouble, run the java app in conjunction with strace - it should show you where you're having trouble in the shell


man strace
0
 
for_yanCommented:
did you try to enclsoe argument in quotes like that:


 /usr/bin/mplayer -slave -quiet "/home/al/music/audio/Slow\ Hand.mp3"

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CEHJCommented:
Try
String[] args = { "/usr/bin/mplayer", "-slave", "-quiet", "/home/al/music/audio/Slow Hand.mp3" };

Open in new window

0
 
for_yanCommented:

Is the name of your file is "Slow Hand.mp3"?

Then first try is it works like that with hardcoded name:
 mplayerProcess = Runtime.getRuntime().exec("/usr/bin/mplayer -slave -quiet \"/home/al/music/audio/Slow Hand.mp3\"");

if it works then it will be easy to devise the way to make it for general case.
Just try fisrt this way and let us know if it works.
0
 
CEHJCommented:
Have just tried it inside your open(File) method and it worked fine there too
0
 
sargent240Author Commented:
I will try your suggestions this evening.  Got a couple of errands then will hit it.  Thanks, I will be in touch.
0
 
sargent240Author Commented:
CEHJ - I ran into one problem, and that is the variable I am sending to open is that of FILE.  I have attached the code I put in open and I get an incompatible types error so some way I need to convert the variable file.  It does not like command anywhere in the code.  What do ya think?
Cheers.
if (mplayerProcess == null) {
            String[] command = {
                    mplayerPath, file
            };
            logger.info("Starting MPlayer process: " + command);
            mplayerProcess = Runtime.getRuntime().exec(command);

Open in new window

0
 
sargent240Author Commented:
I think I figured it out.  I'll know in a bit.
0
 
sargent240Author Commented:
Boy CEHJ, what does your open(file) method look like.  I'm trying to maintain all the message code in tact and am not having much luck.
0
 
sargent240Author Commented:
Got it!  I'll post what I did in a bit, after I get it cleaned up a bit.
0
 
CEHJCommented:
>>logger.info("Starting MPlayer process: " + command);


should be


logger.info("Starting MPlayer process: " + java.util.Arrays.toString(command));

Open in new window

0
 
sargent240Author Commented:
Back at it after a delay to try and make a living.  All seems to be working except I cannot set the volume with the setVolume method.  I cannot compile the code at this time because of a problem elsewhere I need to correct.  I will try to get that handled and get the source to you that I am using at this time.  I'll be back in touch.  THANKS!

Cheers
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.