Solved

Java convert string to file

Posted on 2014-02-16
15
401 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
15 Comments
 

Author Comment

by:sargent240
Comment Utility
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 35

Expert Comment

by:mccarl
Comment Utility
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
Comment Utility
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
 

Author Comment

by:sargent240
Comment Utility
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
Comment Utility
Always use the String[] version of Runtime.exec. Give each part of the command its own element
0
 
LVL 16

Expert Comment

by:Valeri
Comment Utility
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 500 total points
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:sargent240
Comment Utility
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
Comment Utility
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
Comment Utility
The problem is, what you suggest breaks the rest of my code.
0
 

Author Comment

by:sargent240
Comment Utility
All the items in my code works using the original lines.
0
 

Author Comment

by:sargent240
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
:)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now