jmf wav to mp3 conversion issue - java

I am trying to convert a wav file into mp3 format using the below code. A mp3 file is created but it is blank file without any content. Any ideas what could be causing this.
Format[] FORMATS = new Format[] { new AudioFormat(
AudioFormat.LINEAR) };
String srcFilePath = "C:" + File.separator + "Users"
+ File.separator + "vkiran" + File.separator + "Desktop"
+ File.separator + "test" + File.separator + "4a714ea2.wav";
String mp3FilePath = "C:" + File.separator + "Users"
+ File.separator + "vkiran" + File.separator + "Desktop"
+ File.separator + "test" + File.separator + "4a714ea2.mp3";
File srcFile = new File(srcFilePath);
File destFile = new File(mp3FilePath);
MediaLocator source = new MediaLocator(srcFile.toURL());
DataSource d = Manager.createDataSource(source);
ContentDescriptor cd = new ContentDescriptor(
FileTypeDescriptor.MPEG_AUDIO);
Processor p = Manager.createRealizedProcessor(new ProcessorModel(d,
FORMATS, cd));
p.configure();
System.out.println((p.getDuration()).getSeconds());
MediaLocator destination = new MediaLocator(destFile.toURL());
DataSource ds = p.getDataOutput();
DataSink sink = Manager.createDataSink(p.getDataOutput(),
destination);
sink.open();
sink.start();
sink.stop();
sink.close();
p.stop();
p.close();

Related

WhatsApp status in java

When I had the Target API 29, the app was showing WhatsApp statuses, but when I increase the target api to 32, it is not showing the statuses.
public File getWhatsupFolder() {
if (new File(Environment.getExternalStorageDirectory().toString() + File.separator + "Android/media/com.whatsapp/WhatsApp" + File.separator + "Media" + File.separator + ".Statuses").isDirectory()) {
return new File(Environment.getExternalStorageDirectory() + File.separator + "Android/media/com.whatsapp/WhatsApp" + File.separator + "Media" + File.separator + ".Statuses");
} else {
return new File(Environment.getExternalStorageDirectory() + File.separator + "WhatsApp" + File.separator + "Media" + File.separator + ".Statuses");
}
}
Here is the code of my getting status that was working fine in target api 29.
If anyone knows what is the issue. Please let me know. Thanks in advance.

How to call file and run in Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler)

This is my coding and it will create Decrypted image.jpg in desktop directory
File desktopDir = new File(System.getProperty("user.home"), "Desktop");
System.out.println(desktopDir.getPath() + " " + desktopDir.exists());
String pathToDesktop = desktopDir.getPath();
FileOutputStream outStream = new FileOutputStream(new File(desktopDir, "Decrypted image.jpg"));
and how to call that file "Decrypted image.jpg" in
Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler " + "Decrypted image.jpg");
how to change code up there so that can run and show the image ? sorry for bad english.

How to sequentially numbering files rather than overwriting them when moving

I am writing a code that could move the file from one directory to another, but I have an issue with having a file that have the same name, so I decided to number them as I don't want to overwrite them.
Assume I have file a.txt, I succeed to move to move the file with the same name then call it a_1.txt, but I am wondering what I can do if I have again a.txt?
Moreover, I feel my code is not efficient and it will be appreciated if you help me to enhance it.
My code is:
/*
* Method to move a specific file from directory to another
*/
public static void moveFile(String source, String destination) {
File file = new File(source);
String newFilePath = destination + "\\" + file.getName();
File newFile = new File(newFilePath);
if (!newFile.exists()) {
file.renameTo(new File(newFilePath));
} else {
String fileName = FilenameUtils.removeExtension(file.getName());
String extention = FilenameUtils.getExtension(file.getPath());
System.out.println(fileName);
if (isNumeric(fileName.substring(fileName.length() - 1))) {
int fileNum = Integer.parseInt(fileName.substring(fileName.length() - 1));
file.renameTo(new File(destination + "\\" + fileName + ++fileNum + "." + extention));
} else {
file.renameTo(new File(destination + "\\" + fileName + "_1." + extention));
}
}//End else
}
From the main, I called it as the following (Note that ManageFiles is the class name that the method exist in):
String source = "L:\\Test1\\Graduate.JPG";
String destination = "L:\\Test2";
ManageFiles.moveFile(source, destination);
You can use this logic:
If the file already exists in the destination, you add "(1)" to the file name (before the extension). But then you ask me: what if there's already a file with "(1)"? Then you use (2). If there's already one with (2) too, you use (3), and so on.
You can use a loop to acomplish this:
/*
* Method to move a specific file from directory to another
*/
public static void moveFile(String source, String destination) {
File file = new File(source);
String newFilePath = destination + "\\" + file.getName();
File newFile = new File(newFilePath);
String fileName;
String extention;
int fileNum;
int cont;
if (!newFile.exists()) {
file.renameTo(new File(newFilePath));
} else {
cont = 1;
while(newFile.exists()) {
fileName = FilenameUtils.removeExtension(file.getName());
extention = FilenameUtils.getExtension(file.getPath());
System.out.println(fileName);
newFile = new File(destination + "\\" + fileName + "(" + cont++ + ")" + extention);
}
newFile.createNewFile();
}//End else
}

Image rename error using java

Here i'm able to change the name of image but for the new image i'm not getting the same image as first one.
Here my initial image is ABC.png and new image is XYZ.png i'm able to change the image name from ABC.png to XYZ.png but i need XYZ.png as similar to ABC.png just i need to change name of image.
Here is my code
File prevfile = new File(ImagesPath + "/" + oldvalue + ".png");
File nextfile = new File(ImagesPath + "/" + newValue + ".png");
prevfile.renameTo(nextfile);
File prevfile = new File(ImagesPath + "/" + oldvalue + ".png");
File nextfile = new File(ImagesPath + "/" + newValue + ".png");
//prevfile.renameTo(nextfile);
Rather than renameTo function better use
FileUtils.copyFile(prevfile , nextfile );
prevfile .delete();
Rename Image with new name and delete previous name.

Construct File Path using Java

I am trying to create a file, but file path is constructed through String Concats that uses some internal variables and labels, I am getting following error :
Exception in thread "main" java.io.IOException: The filename, directory name, or volume label syntax is incorrect
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at CopyEJ.CopyEJ.main(CopyEJ.java:133)
Is there a standard approach to built such files ?
String s_path = text_dir + "\\" + time_stmp + "_" + "Session" + "_" + file_name;
File ssw = new File(s_path);
ssw.createNewFile(); //Errors Out here
You need to create the folder (directory) first:
String s_path = text_dir + "/" + time_stmp + "_" + "Session" + "_" + file_name;
File ssw = new File(s_path);
ssw.getParentFile().mkdirs();
ssw.createNewFile();
This will help you:
String path = "D://abc" + filename+ ".txt";
System.out.println("Path--- " + path);
File file = new File(path);
file.createNewFile()
If You are using Java 1.7 you can rewrite it as follows:
Path s_path = Paths.get(text_dir, time_stmp + "_" + "Session" + "_" + file_name);
Files.createDirectories(s_path.getParent());
File ssw = s_path.toFile();
ssw.createNewFile();
Paths.get() will use default system path separator.

Categories