Open mp3 file using specific applications java - java

I want to know if there is a way of opening mp3 files using either Windows Media Player and VLC Media Player.
I have the path of the file saved as a String and was wondering if I can use this to open the file on either of the applications mentioned above.
I understand one method of doing this is by using the Desktop class but this opens the file using the default application, which is Windows Media Player in my case. How would I open the file in VLC media player?

try
Runtime.getRuntime().exec("<your vlc path> <your file>");
I.E.
Runtime.getRuntime().exec("\"C:\\Program Files (x86)\\VideoLAN\VLC\\vlc.exe\" abc.mp3");
Tried and it works

Judging by the Desktop.open apidoc and
Desktop tutorial it looks like the open method can only open the file in their default associated program.
So, you have (maybe) two options here:
access the registry and change the default association (not a nice one)
might try giving a URI and opening it via browser, which might result in opening the file in desired program. However, it looks like the file scheme is the only acceptable solution, and that will most probably open in the default program. There is a mms scheme but it is used for video-streaming, and again, it will most probably open in the default program.
Here is a link to the Runtime.exec solution for windows, linux and osx variant.

You should know the path to the VLC and/or WMP. Then you can use Runtime class for this.
Process p = Runtime.getRuntime().exec("c:/vlc/vlc.exe " + mp3FileName);

Related

Is there a function like ShellExecute in Java?

Im just looking for function that can run some .exe files
But I didn't find that function in Java
ShellExecute(Operation, File, Parameters, Directory, ShowCmd : String) is there a function like that?
java.awt.Desktop provides some of that functionality:
browse can be used to open the default browser for a specific URI
open opens a file for viewing (with the default viewer of the OS)
edit opens a file for editing (with the default editor of the OS)
...
If you want to run arbitrary exe files, however, then Runtime.exec is the way to go (but using it can be tricky, as you need to consume all streams for it to not lock up).

how can I get the parameter passed from the OS to the application and then use it

I have developed my java application and deployed it.
I want it to detect when i double click a music file eg .mp4, .mp3 and others to open play.
I have already set it as my default but it just opens the app, it cannot pick the path of the file.
Colleagues, how do I get that file path and use it when it is called?
Thanks a lot.

How do you open a system file explorer in java to select a file for saving or loading?

I am trying to create a simple pixel art drawing application in java. Right now to load and save a file you have to use the console and input the path. I'm wondering how would I open a file explorer and allow the user to select a file through it. I have used a JFileChooser in the past but I'm asking about how to open the OS file explorer. Thanks!
EDIT: I guess I was confusing the first time. I don't want to just open a file explorer I want to be able to have my program interact with it and allow the user to select a file with it. Like this:
EDIT: So I found FileDialog which seems to do what I want except that it is designed for AWT and I am using OpenGL. I can try and make that work but if andyone has anything that works with lwjgl that would be greatly appreciated.
For a cross platform solution, you could use the Desktop API, see How to Integrate with the Desktop Class for more details
Maybe something like...
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
desktop.browse(uri); // Throws
}
When running on Windows, I tend to use something like...
String path = file.getCanonicalPath();
ProcessBuilder pb = new ProcessBuilder("explorer.exe", "/select," + path);
pb.redirectError();
Process proc = pb.start();
which will highlight the specified file, but is only suitable when running on Windows
I tend to switch between the two based on the platform the app is running on

How can I determine the way to access certain third-party programs and resources from my Java app?

I'm trying to open a PDF file after I generate a report. I mean, the user logs in (it's a Swing-based app) and clicks to generate a report. Then, a PDF file is generated. I would like to launch the PDF reader at that moment. I could do something like exec("evince "+path_to_pdf_file). It's just for Ubuntu, Windows would be more difficult. I'm thinking I need to explore the registry.
How can I achieve this?
What you need is the method java.awt.Desktop#open
Launches the associated application to open the file.
If the specified file is a directory, the file manager of the current platform is launched to open it.

Java Desktop API not working with network paths? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Not possible to launch a file on a network using Java Desktop?
I am trying to use the Desktop API to launch the appropriate app for a file. So i am using this :
if (Desktop.isDesktopSupported())
Desktop.getDesktop().open(new File(path));
where "path" is a String pointing to the file.
Everything works fine until i try to launch a jpg that resides at a network location (for instance "\\MyNet\folder\image.jpg") when i get an IOException :
java.io.IOException: Failed to open
file:////MyNet/folder/image.jpg
Any one knows if there is a way to fix this?
I believe you need to specify the file location/name in standard URI format - which is close to the standard format except for servers. See the javadocs for the URI Class for more information.
At the highest level a URI reference (hereinafter simply "URI") in string form has the syntax
[scheme:]scheme-specific-part[#fragment]
And a little later:
A hierarchical URI is subject to further parsing according to the syntax
[scheme:][//authority][path][?query][#fragment]
so the URI should look something like the following:
file://MyNet/folder/image.jpg
where "file://" is the protocol, "MyNet" is the server, and "/folder/image.jpg" is the directory location under the share.
Hope this helps a little.
file:////MyNet/folder/image.jpg is not a file path. It's an URL.
File f = new File("\\\\192.168.0.4\\mybookrw\\save\\command.txt");
Desktop.getDesktop().open(f);
Worked fine for me. The one caveat is that you have to be authenticated against the share already. If you paste the path into the run box and it prompts you for a username and password then its not going to work from an app.
Everyone so far has assumed that the file isn't being found.
However, looking at the Desktop open() function, an IOException is thrown
if the specified file has no associated
application or the associated
application fails to be launched
Now, having said that, what happens if you open a jpg on your local machine? Also, what happens if you try manually launching the jpg through the network?
Edit: Actually, the problem may be that the default program set to open jpg files doesn't understand file:// uris. Sticking with UNC paths might be a better choice.

Categories

Resources