Java detecting an audio file (mp3) - java

I have this code that reads an mp3 file
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Sound {
public static void main(String[] args) {
File sampleFile = new File("test.mp3");
try {
AudioSystem.getAudioFileFormat(sampleFile);
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The problem here is that it is returning file not supported exception, the file here is an mp3 file. Java doesn't support mp3 files? if so what are others to validate an audio file?(like ogg, wav)

You may take a look at Apache Tika library. It can detect type of a file by its content and extract file metadata. It supports mp3 format.
Here is an example of file type detection with Apache Tika.

You need to add MP3SPI library so that java audio api could recognize and decode mp3 files.

Related

Create a PDF file using LaTex templates and Java

I need to create a report system using LaTex templates and Java like programming language. I use JLR library but it's freeeware, this is my code:
File workingDirectory = new File("./config/output");
File desktop = new File("./config/desktop");
File invoice1 = new File("./config/templates/template1.tex");
File invoice2 = new File("./config/templates/template2.tex");
JLRGenerator pdfGen = new JLRGenerator();
pdfGen.deleteTempTexFile(false);
if (!pdfGen.generate(invoice1, desktop, workingDirectory)) {
System.out.println(pdfGen.getErrorMessage());
}
JLROpener.open(pdfGen.getPDF());
if (!pdfGen.generate(invoice2, desktop, workingDirectory)) {
System.out.println(pdfGen.getErrorMessage());
}
JLROpener.open(pdfGen.getPDF());
Searching into the Web I find JLatexMath but, as far as I know, only generates equations in LaTex and not the entire PDF. Do you know a library in Java which generates an entire PDF file using a LaTex template?
Update: I execute de .tex file using Runtime.getRuntime().exec("pdflatex.exe...") command. But I don't achive save the PDF file.
Thanks in advance
I think I have the solution, here is the code:
public void generateReport()
{
Process p;
try {
p = Runtime.getRuntime().exec("C:\\pdflatex.exe -synctex=1 -interaction=nonstopmode ./config/log/document.tex");
p.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
It generates a .PDF and .dvi file in the same place of your .tex file. Thank you much for your help :)

Tika slower in my Java app than in TikaJAXRS

I'm trying to make use of Tika from a C# project that needs to extract text from a large volume of files.
I started with a simple proof of concept that made use of TikaJAXRS, reading the content of the files and making a HTTP PUT request with the file content to the TikaJAXRS server at http://localhost:9998/tika. This works reasonably well, but it struck me that the overhead of streaming content through HTTP must be slowing things down.
So I decided to write a Java implementation to see how the performance would compare once HTTP is removed from the equation. What I've found is unexpected. It performs much slower, taking roughly twice as long to parse 65 files of various types totaling 16MB. 1200ms for the TikaJAXRS HTTP scenario, 2400ms for the Java app.
Both the TikaJAXRS server and the Tika libraries I'm using are version 1.7. My Java code listing is below. What am I missing, why is my Java app so much slower?
import org.apache.log4j.varia.NullAppender;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.lang3.time.StopWatch;
public class TikaTest {
public static void main(String[] args) {
// I'm not interested in what log4j has to say...
org.apache.log4j.BasicConfigurator.configure(new NullAppender());
File folder = new File("C:\\LMDevelopment");
StopWatch timer = new StopWatch();
timer.start();
Collection<File> files = FileUtils.listFiles(folder, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
Tika tikaClient = new Tika();
try {
tikaClient.parseToString(files.iterator().next());
} catch (IOException e) {
e.printStackTrace();
} catch (TikaException e) {
e.printStackTrace();
}
System.out.println("Time to warm up: " + timer.getTime() + "ms");
timer.reset();
timer.start();
for (File f : files)
{
try {
tikaClient.parseToString(f);
} catch (IOException e) {
e.printStackTrace();
} catch (TikaException e) {
e.printStackTrace();
}
}
timer.stop();
System.out.println("Time to parse all files: " + timer.getTime() + "ms");
}
}

Playing Sound in Java [duplicate]

This question already has answers here:
Access restriction error on my Java Code
(2 answers)
Closed 8 years ago.
Hello I'm trying to play a sound in java the code looks like this:
public void playSound(String sound) {
try {
InputStream in = new FileInputStream(new File(sound));
AudioStream audio = new AudioStream(in);
AudioPlayer.player.start(audio);
} catch (Exception e) {}
}
I imported sun.audio*; however get an error:
Access restriction: The type 'AudioPlayer' is not API (restriction on
required library 'C:\Program Files\Java\jre7\lib\rt.jar')
The following program plays a 16-bit wav sound from eclipse if we use javax.sound.
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
import javax.swing.*;
// To play sound using Clip, the process need to be alive.
// Hence, we use a Swing application.
public class SoundClipTest extends JFrame {
// Constructor
public SoundClipTest() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Test Sound Clip");
this.setSize(300, 200);
this.setVisible(true);
// You could also get the sound file with a URL
File soundFile = new File("C:/Users/niklas/workspace/assets/Sound/sound.wav");
try ( // Open an audio input stream.
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
// Get a sound clip resource.
Clip clip = AudioSystem.getClip()) {
// Open audio clip and load samples from the audio input stream.
clip.open(audioIn);
clip.start();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new SoundClipTest();
}
}

Reading word file and saving it as odt

I have downloaded ODFToolkit, and I don't have to setup OpenOffice. I can create odt file as following.
And my question is - May I read .doc and .docx files and save them as .odt ?
Could you help me please?
Here is the code:
import org.odftoolkit.odfdom.doc.OdfTextDocument;
import java.net.URI;
public class QuickOdt {
public static void main(String[] args) {
OdfTextDocument outputDocument;
try {
outputDocument = OdfTextDocument.newTextDocument();
outputDocument.addText("I'm using the ODFDOM toolkit!");
outputDocument.newParagraph();
outputDocument.newImage(new URI("images/odf-community.jpg"));
outputDocument.newParagraph("Bu bir taze paragrafdyr");
outputDocument.save("quick.odt");
} catch (Exception e) {
System.err.println("Unable to create output file.");
System.err.println(e.getMessage());
}
}
}
You should look into Apache POI for reading word documents, and Apache OpenOffice Writer for writing into odt format.

Get readable text only from clipboard

I already know how to get plain text from the clipboard in Java, but sometimes the text is encoded in some weird DataFlavor, like when copying from Microsoft Word or from a website or even source code from Eclipse.
How to extract pure plain text from these DataFlavors?
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
String data = (String) Toolkit.getDefaultToolkit()
.getSystemClipboard().getData(DataFlavor.stringFlavor);
with the getData() Method and the stringFlavor you should get plain text from the clipboard.
If there are weird text in the clipboard, I think, this should be a problem of the program which puts the data in the clipboard.
You can use following method getting clipboard text in Java:
public String getClipBoard(){
try {
return (String)Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
} catch (HeadlessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedFlavorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
First I haven't worked with clipboard but this seems intresting
From http://docstore.mik.ua/orelly/java/awt/ch16_01.htm
"To read data from the clipboard, a program calls the Transferable.getTransferData() method. If the data is represented by a DataFlavor that doesn't correspond to a Java class (for example, plainTextFlavor), getTransferData() returns an InputStream for you to read the data from."
So if you give it a class which doesn't correspont you get the InputStream and then you can read the "pure" text from the InputStream yourself.

Categories