Now I am trying with another sound file with mp3 codec. But I am encounterring problem to start the player.
I have downloaded the file from http://www.mediacollege.com/audio/tone/download/
The output from Netbeans is:
run: Error: Unable to realize com.sun.media.amovie.AMController#ebf3f0 BUILD SUCCESSFUL (total time: 2 seconds)
And my program is:
import javax.media.*;
import java.io.*;
public class MP3Player {
public static void main(String[] args) throws Exception
{
File file = new File("c:/player/sound.mp3");
MediaLocator mrl = new MediaLocator(file.toURL());
Player player = Manager.createPlayer(mrl);
player.start();
Thread.sleep(1000);
}
}
JMF does not support the mp3 format due to licensing issues.
This article on extending JavaSound to Play MP3 should give you everything that you need, including a place to download an mp3 codec. I have recently used this sample code as the basis for an mp3 player, and used it on both Windows and OS X, and I can verify that it works very well.
Try using this:
String theFile = "file:///c:/player/sound.mp3";
MediaLocator mrl = new MediaLocator(theFile);
works good for me..
Related
I am having some problems with this code, I am trying to play the sound file but it is not working.
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
public class DiscoMusic
{
public static void main(String[] args) throws Exception
{
URL url = new URL("http://www.lecons-guitare.com/Audio/Backingtracks/Stayin_alive.mp3");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
}
}
According to the JavaSound info. page.
MP3 decoding support
The Java Sound API does not support many formats of sampled sound
internally. In a 1.6.0_24 Oracle JRE getAudioFileTypes() will
generally return {WAVE, AU, AIFF}. An MP3 decoder at least, is close
by. The mp3plugin.jar of the Java Media Framework supports decoding
MP3s.
I can vouch for that information since I've successfully loaded MP3s using Java Sound and the MP3 SPI (& also wrote the info. page ;) ). The JMF installer download is becoming hard to find, but you can get the mp3plugin.jar direct from where I put it for use in JWS apps.
I was trying to use MP3s instead of wavs and it works fine in NetBeans, but when I build it and try to run the jar there's no sound and I get the NoPlayerException.
background = ImageIO.read(getClass().getResourceAsStream("/background1.png"));
sun = ImageIO.read(getClass().getResourceAsStream("/sun.png"));
cloud = ImageIO.read(getClass().getResourceAsStream("/cloud.png"));
pause = ImageIO.read(getClass().getResourceAsStream("/pause.png"));
soldierchant = AudioSystem.getAudioInputStream(getClass().getResource("/SoldiersChant.wav"));
thebreach = AudioSystem.getAudioInputStream(getClass().getResource("/TheBreach.wav"));
forever = AudioSystem.getAudioInputStream(getClass().getResource("/Forever.wav"));
Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
Format input2 = new AudioFormat(AudioFormat.MPEG);
Format output = new AudioFormat(AudioFormat.LINEAR);
PlugInManager.addPlugIn("com.sun.media.codec.audio.mp3.JavaDecoder", new Format[]{input1, input2}, new Format[]{output}, PlugInManager.CODEC);
try {
Player player = Manager.createPlayer(new MediaLocator(getClass().getResource("/TheBreach.mp3").toURI().toURL()));
player.start();
} catch (IOException ex) {
ex.printStackTrace();
}catch (java.net.URISyntaxException x) {
x.printStackTrace();
}catch (javax.media.NoPlayerException c) {
c.printStackTrace();
}
As you can see I'm getting the file from using getResource just like the images and wav that work both in NetBeans and jar. Could this be the problem com.sun.media.codec.audio.mp3.JavaDecoder
I'm grabbing at straws at this point. I've tried putting the MP3 in every folder and doing no / and the full directory.
This is my Exception's getMessage:
Cannot find a Player for jar:file:/C:/Users/Patrick/Documents/NetBeansProjects/PatBuild8FX/dist/PatBuild8FX.jar!/TheBreach.mp3
I think it is Manager.createPlayer because when I just create the file alone its fine, but when I try to create a player with it, it doesn't work.
File m = new File("file:/C:/Users/Patrick/Documents/NetBeansProjects/PatBuild8FX/dist/PatBuild8FX.jar/TheBreach.mp3");
System.out.println(m);
System.out.println(Manager.createPlayer(m.toURI().toURL()));
file:\C:\Users\Patrick\Documents\NetBeansProjects\PatBuild8FX\dist\PatBuild8FX.jar\TheBreach.mp3
java.io.IOException: File Not Found
java.io.IOException: File Not Found
Exception in thread "main" javax.media.NoPlayerException: Error instantiating class: com.sun.media.protocol.file.DataSource : java.io.IOException: File Not Found
at javax.media.Manager.createPlayerForContent(Manager.java:1362)
at javax.media.Manager.createPlayer(Manager.java:417)
at javax.media.Manager.createPlayer(Manager.java:332)
at Build8.PanGame.<init>(PanGame.java:75)
at Build8.Main.main(Main.java:30)
Did you package the 'TheBreach.mp3' file into your jar file? The exception message above suggests that the resource is being accessed from your jar file.
My suggestions:
if you haven't already done so, package the .mp3 file into the jar file (this makes it easier for you to move the single jar file without having to copy the .mp3 file along with it, and also the mp3 file should now be accessible from the jar file), or
if you prefer to have the .mp3 file where it is, then try using a java.io.File class to load the mp3 file before passing to the MediaLocator constructor. Hence, you'd then have something like:
new MediaLocator(new File("file:///C:\\full_path_to_the_file\\TheBreach.mp3").toURL())
finally, the documentation for MediaLocator.createPlayer() explaining issues with creating players should be handy. You can find it here
Good luck!
Well, there is FFMPEG and some Java bindings and wrappers for it but I need to distribute for each specific platform the right binary file of FFMPEG.
Isnt there any plain Java solution or library without any dependencies like FFMPEG for converting a video fle to an image sequence?
Solutions like FFMPEG, XUGGLER or JMF (abandoned) are not suitable. Is there really no pure Java solution for this?
Maybe for specific video codecs / files at least?
I just want to extract the images from the video file to jpeg / png files and save them to the disk
Is there really no pure Java solution for [extracting images from a video stream]?
Let's see. You have to:
Decode the video.
Present the decoded images at least as fast as 24 images / second. I suppose you can skip this step.
Save the decoded images.
It appears that decoding the video would be the most challenging step. People and companies have spent years developing codecs (encoder / decoder) for various video formats.
There's a project on SourceForge, JMF wrapper for ffmpeg, that has developed a few pure Java video codecs. Perhaps you can look at their source code and see how to develop a Java video codec for yourself.
You can look for other pure Java video codecs if you wish.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import javax.imageio.ImageIO;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FrameGrabber.Exception;
public class Read{
public static void main(String []args) throws IOException, Exception, InterruptedException, ExecutionException
{
FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber("C:/Users/Digilog/Downloads/Test.mp4");
frameGrabber.start();
IplImage i;
try {
for(int ii=0;ii<frameGrabber.getLengthInFrames();ii++){
i = frameGrabber.grab();
BufferedImage bi = i.getBufferedImage();
String path = "D:/Image/Image"+ii+".png";
ImageIO.write(bi,"png", new File(path));
}
frameGrabber.stop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
There is a pure Java implementation of the following codecs: H.264 ( AVC ), MPEG 1/2, Apple ProRes, JPEG; and the following file formats: MP4 ( ISO BMF, QuickTime ), Matroska, MPEG PS and MPEG TS.
The library is called JCodec ( http://www.jcodec.org ).
It has very little documentation for now but the development team is constantly working on this.
Here's how you can simply grab a frame from an MP4 file ( sample from their web site ):
int frameNumber = 150;
BufferedImage frame = FrameGrab.getFrame(new File("filename.mp4"), frameNumber);
ImageIO.write(frame, "png", new File("frame_150.png"));
To add JCodec to your project you can simply add below to your pom.xml:
<dependency>
<groupId>org.jcodec</groupId>
<artifactId>jcodec</artifactId>
<version>0.1.3</version>
</dependency>
For latest version, see here.
I am trying to run a sound file in Java using this code:
public class Audio3
{
public static void main(String[] args) throws Exception
{
URL soundFile =new URL(
"http://everyayah.com/data/Ghamadi_40kbps/audhubillah.mp3");
AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
AudioPlayer.player.start(ais);
}
}
I am getting this exception
javax.sound.sampled.UnsupportedAudioFileException:
could not get audio input stream from input URL
Any idea what could be the reason?
According to the JavaSound info. page.
MP3 decoding support
The Java Sound API does not support many formats of sampled sound internally. In a 1.6.0_24 Oracle JRE getAudioFileTypes() will generally return {WAVE, AU, AIFF}. An MP3 decoder at least, is close by. The mp3plugin.jar of the Java Media Framework supports decoding MP3s.
I can vouch for that information since I've successfully loaded MP3s using Java Sound and the MP3 SPI (& also wrote the info. page ;) ). The JMF installer download is becoming hard to find, but you can get the mp3plugin.jar direct from where I put it for use in JWS apps.
I am making a small game in Java. For this game I just added sounds. So I want to have all my images and audio files in the jar. For pictures this was easy:
new ImageIcon(Main.class.getResource("images/machgd2.png")).getImage()
But for audio it only works when I run the program in Eclipse but not from a jar. I use:
File soundFile = new File(Main.class.getResource(filename).getFile());
So how can I get this file from inside the .jar file?
Update:
OK, got it working, thanks to Andrew!
To play the sound I used a class I found on the net, and I found out that class just uses File to get an AudioInputStream, so I dropped the File thing.
When it's in a jar file, it isn't a file on the file system, is it? You'll either have to copy the file out of the jar file into some temporary location, or use APIs which don't require a file (e.g. ones which only need an InputStream or a URL, both of which are easily available from jar files using getResourceAsStream or getResource.).
You haven't shown where you're using soundFile - if you show us which APIs you're trying to use, we can try to suggest an appropriate alternative.
See "Playing a Clip" from the Java Sound info page here at SO to see..
import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;
public class LoopSound {
public static void main(String[] args) throws Exception {
URL url = new URL(
"http://pscode.org/media/leftright.wav");
Clip clip = AudioSystem.getClip();
// getAudioInputStream() also accepts a File or InputStream
AudioInputStream ais = AudioSystem.
getAudioInputStream( url );
clip.open(ais);
// loop continuously
clip.loop(-1);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// A GUI element to prevent the Clip's daemon Thread
// from terminating at the end of the main()
JOptionPane.showMessageDialog(null, "Close to exit!");
}
});
}
}
Which, you might notice, uses an URL (as returned by getResource()) rather than a File. The method is overloaded to also accept File or InputStream, but I use the URL based version most commonly.