[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

Java convert string to file

I need to convert a string to a file name ie:

/home/al/music/slow hand.mp3

is a string and I need to convert it to a file type for my audio player.

Thanks.
0
sargent240
Asked:
sargent240
1 Solution
 
sargent240Author Commented:
I figured out how to convert the string to a file.  My problem now is using the tile to play the song.  When I send the file name to my player which is:

        jmPlayer.open(new File("/netgear/music/60sMusic/Abraham, Martin, and John.mp3"));

The following errors appear.  I think I need to do something with the title of the song to accomodate the spaces and punctuation.



INFO: Reading line: File not found: '/netgear/music/60sMusic/Abraham,'
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line: Playing /netgear/music/60sMusic/Abraham,.
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line: Failed to open /netgear/music/60sMusic/Abraham,.
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line:
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line: File not found: 'Martin,'
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line:
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line: Failed to open Martin,.
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line: Playing Martin,.
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line: File not found: 'and'
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line:
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line: Failed to open and.
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line:
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line: Playing and.
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line:
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line: File not found: 'John.mp3'
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line:
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line: Failed to open John.mp3.
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line: Playing John.mp3.
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line:
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line:
Feb 16, 2014 1:36:56 PM jmplayer.JMPlayer waitForAnswer
INFO: Reading line: Exiting... (End of file)
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
That would need to be handled in your JMPlayer code. And your current code appears to be different to what you have posted in previous questions. Can you post the latest code?
0
 
CEHJCommented:
File f = new File("/netgear/music/60sMusic/Abraham, Martin, and John.mp3");
boolean gotItRight = f.exists();

Open in new window

Will tell you if your path is correct
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
sargent240Author Commented:
I AM INCLUDING TWO SEGMENTS OF CODE.  THE FIRST SEGMENT IS JUST BELOW AND THE SECOND BELOW THAT.  THE CODE JUST BELOW THIS LINE WILL NOT WORK IF THERE ARE SPACES IN THE FILE NAME.  THE CODE BELOW THIS FIRST SEGMENT USES A STRING AND WILL PLAY A FILE WITH SPACES AND PUNCTUATION IN THE FILE NAME.

    public void open(File file) throws IOException {
        String path = file.getPath();

        if (mplayerProcess == null) {
            // start MPlayer as an external process
            String command = mplayerPath + mplayerOptions + path;
            logger.info("Starting MPlayer process: " + 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);
    }


CODE SEGMENT THAT WILL PLAY A FILE WITH SPACES AND PUNCTUATION IN THE FILE NAME.


    public void open(String audioCut) throws IOException {

        con = (Connection) connectDatabase.getConnection();
       
        try {
            Statement st = (Statement) con.createStatement();
            ResultSet rec = st.executeQuery ("SELECT * FROM audio_cuts WHERE title=" +  "'" + audioCut + "' ");

            rec.next();
            file = rec.getString("file_name");
        } catch (SQLException ex) {
               
            JOptionPane.showMessageDialog(null,
                "SQLException getting file name: " + ex.getMessage());
            return;
        }
       
        if (mplayerProcess == null) {
//            if (i == 0) ++i;
            String[] command = {
               mplayerPath, mplayerOptions, file };
            logger.info("Starting MPlayer process: " + command);
            mplayerProcess = Runtime.getRuntime().exec(command);
//            Process saveProcess= mplayerProcess;
            // 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 \"" + file + "\" 0");
        }    
        // wait to start playing
        waitForAnswer("Starting playback...");

    }
0
 
CEHJCommented:
Always use the String[] version of Runtime.exec. Give each part of the command its own element
0
 
ValeriCommented:
the problem is that currently mplayer don't play files with spaces. try this fix:
go to /usr/share/applications/mplayer.desktop and change
'Exec=gmplayer %U' to 'Exec=gmplayer %F'
this should fix your problem
0
 
CEHJCommented:
the problem is that currently mplayer don't play files with spaces.
That might be the case (pretty dumb if so) but no shell will correctly handle an argument containing spaces unless those spaces are escaped or the argument is quoted. So if you're passing to the shell

 String command = mplayerPath + mplayerOptions + path;

Open in new window


what you actually need is

 String[] command = { mplayerPath, mplayerOptions, path };

Open in new window

and if even that doesn't work, try
 String[] command = { mplayerPath, mplayerOptions, new File(path).toURI().toString() };

Open in new window

0
 
sargent240Author Commented:
I am attaching my program.  If you have mplayer installed and running and execute the code I have attached it will play an audio file you have coded into the program.  You will need an audio file to play.  You will see early in the open method I have included the lines you suggest.  the first line you suggested:

 String[] command = { mplayerPath, mplayerOptions, path };

will play an the audio file listed in the source code.  I did not test it with spaces as when I use the above line you suggested I loose control of the volume.  If I use the original line in the code:

String command = mplayerPath + mplayerOptions + path;

it will not play a file with spaces or punctuation but I have control of the volume as well as some of the other functions.

If I uncomment and use the line of code you suggest:

 String[] command = { mplayerPath, mplayerOptions, new File(path).toURI().toString() };

The file will not play.
jmplayer3.java
0
 
CEHJCommented:
the first line you suggested:

 String[] command = { mplayerPath, mplayerOptions, path };

will play an the audio file listed in the source code.  I did not test it with spaces
I did test it - and it does.

So you now have a different problem with volume control, which should be addressed as a separate question.
0
 
sargent240Author Commented:
The problem is, what you suggest breaks the rest of my code.
0
 
sargent240Author Commented:
All the items in my code works using the original lines.
0
 
sargent240Author Commented:
In my original post my question suggests a problem with spaces and punctuation in the file name.  In my posting number 39865161 I incorrectly suggested under certain circumstances the code worked with spaces and punctuation.  The code worked with spaces but not punctuation such as a hyphen.
0
 
sargent240Author Commented:
The problem with the punctuation, I discover is with the getting the file name from the mysql database and of course that is another issue all together.
0
 
sargent240Author Commented:
Technically speaking the selected solution is the answer to my original posting although other parts of the code cease to function.
0
 
CEHJCommented:
:)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now