?
Solved

Java convert string to file

Posted on 2014-02-16
15
Medium Priority
?
423 Views
Last Modified: 2014-02-21
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
Comment
Question by:sargent240
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 

Author Comment

by:sargent240
ID: 39863898
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
 
LVL 36

Expert Comment

by:mccarl
ID: 39864022
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 39864492
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.

 

Author Comment

by:sargent240
ID: 39865161
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 39865214
Always use the String[] version of Runtime.exec. Give each part of the command its own element
0
 
LVL 16

Expert Comment

by:Valeri
ID: 39866835
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 39867023
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
 

Author Comment

by:sargent240
ID: 39872281
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 39873129
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
 

Author Comment

by:sargent240
ID: 39875807
The problem is, what you suggest breaks the rest of my code.
0
 

Author Comment

by:sargent240
ID: 39875811
All the items in my code works using the original lines.
0
 

Author Comment

by:sargent240
ID: 39877419
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
 

Author Comment

by:sargent240
ID: 39877427
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
 

Author Closing Comment

by:sargent240
ID: 39877439
Technically speaking the selected solution is the answer to my original posting although other parts of the code cease to function.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39877540
:)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question