AudioPlayer not playing sound on a mac with java 1.6 - java

I wrote a media player, that I exported as a jar file.
It works great on my linux system. Compiled with 1.8, but to work with 1.6 as well.
Now my friend who needs to use it runs the jar on her mac (java 1.6) and the program works, does not throw an exception, but does not play sound.
Any ideas what could have gone wrong?
public void mediaPlayer()
throws Exception {
// open the sound file as a Java input stream
String soundFile = "./data/1.wav";
InputStream in = new FileInputStream(soundFile);
//
// // create an audiostream from the inputstream
AudioStream audioStream = new AudioStream(in);
// // play the audio clip with the audioplayer class
AudioPlayer.player.start(audioStream);
}

Oh, the data folder was misplaced. Solved.

Related

Java - Sound Won't Load - AudioInputStream [duplicate]

The message on the shell is:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid format
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.createStream(PulseAudioDataLine.java:142)
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:99)
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:283)
at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:402)
at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:453)
at reprod.ReproducirFichero(reprod.java:16)
at reprod.main(reprod.java:44)
I try to download new drivers for audio, i try to reinstall openJDK 7 and openJRE 7 and also i try to install java 7.
I have proved my code in another computer and it works, the desktop board that i use is an intel d525mw, the audio format that i´m trying to play is .wav.The version of linux that I use is Ubuntu 12.04.3.Please I need help.Thanks
here is party of my code, and i try to play a .wav audio format
import javax.sound.sampled.*;
public class reprod {
public static void play(){
try {
Clip cl = AudioSystem.getClip();
File f = new File("/home/usr/Desktop/d.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(f);
cl.open(ais);
cl.start();
System.out.println("playing...");
while (cl.isRunning())
Thread.sleep(4000);
cl.close();
the version of linux that I use is Ubuntu 12.04.3
I solved the problem by simply passing the parameter null into AudioSystem.getClip().
I don't know why this exception occured, I run this project before on Windows, and it worked... After on Linux and here, it didn't work.
I had the same problem and found this code to work:
File soundFile = new File("/home/usr/Desktop/d.wav");
AudioInputStream soundIn = AudioSystem.getAudioInputStream(soundFile);
AudioFormat format = soundIn.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip)AudioSystem.getLine(info);
clip.open(soundIn);
clip.start();
while(clip.isRunning())
{
Thread.yield();
}
The key is in soundIn.getFormat(). To quote the docs:
Obtains the audio format of the sound data in this audio input stream.
Source: http://ubuntuforums.org/showthread.php?t=1469572
The error message says that the input file format is wrong somehow.
If you gave us more information (file format, maybe where you got it, code that you use to open the file and how you configured the audio drivers), we might be able to help.
See this question for some code that you can try: How to play .wav files with java

How does one loop audio files in Java?

This is my code
public static void main(String[] args) {
try{
// open the sound file as a Java input stream
InputStream in = new FileInputStream("Sound.wav");
// create an audiostream from the inputstream
AudioStream audioStream = new AudioStream(in);
// play the audio clip with the audioplayer class
AudioPlayer.player.start(audioStream);
}
catch(Exception e){
System.out.println("Audio not found");
}
}
Sun Audio is Undocumented
You should not be using Sun Audio in your program. See here and here.
The Answer Depends on Your Library
If you are using Java Clip (built-in), try the example here. This is probably the simplest method.
If you switch to use JavaFX, you can see the API here, which has ways to loop. However, JavaFX requires a bit of package restructuring, and there is a learning curve. Unless you need JavaFX for other reasons, I would avoid it.
If you are using any other library, please consult the API for that library, or share with us what you are attempting to use. I have personally used BasicPlayer, but it is quite old.

How to play a .mp3 file successfully [duplicate]

This question already has answers here:
Playing .mp3 and .wav in Java?
(15 answers)
Closed 8 years ago.
I want to play an audio file when certain conditions are met but I can't figure it out. I am inexperienced in java and programming in general and copied several blocks of code and none of them work.
Trying to play this:
File type: .mp3
Location: C:\Users\Public\Music\Sample Music
I wasted close to two hours on this. Plz help with code that just plays audio from a file. Thank you.
Update: I keep getting this error
Multiple markers at this line
- Access restriction: The type AudioStream is not accessible due to restriction on required library C:\Program Files (x86)\Java\jre1.8.0_25\lib\rt.jar
- Access restriction: The type AudioStream is not accessible due to restriction on required library C:\Program Files (x86)\Java\jre1.8.0_25\lib\rt.jar
- Access restriction: The constructor AudioStream(InputStream) is not accessible due to restriction on required library C:\Program Files (x86)\Java
Check Xuggle or jLayer.
Google them and you will come up with nice results.
Good >> http://kxhitiz.blogspot.com.tr/2010/09/playing-mp3-in-java-programming.html
Java Jlayer Mp3 Player - how to repeat and stop song
public class JavaAudioPlaySoundExample
{
public static void main(String[] args)
throws Exception
{
// open the sound file as a Java input stream
String gongFile = "/Users/al/DevDaily/Projects/MeditationApp/resources/gong.au";
InputStream in = new FileInputStream(gongFile);
// create an audiostream from the inputstream
AudioStream audioStream = new AudioStream(in);
// play the audio clip with the audioplayer class
AudioPlayer.player.start(audioStream);
}
}

File found in eclipse but not when I run the .jar in terminal

I have a program which has to play sounds from a terminal interface.
The code is fairly simple and here it is :
public static synchronized void playSound() {
new Thread(new Runnable() {
public void run() {
File _file = new File("music/sound.wav");
try (AudioInputStream _audio = AudioSystem
.getAudioInputStream(_file)) {
Clip _clip = AudioSystem.getClip();
_clip.open(_audio);
_clip.start();
} catch ([…] e) {
// […]
}
}
}).start();
}
The file is in the music folder which is in my source path.
All work perfectly well when I run the program in eclipse. But if I export it in a .jar file and try it in the windows cmd I get this message :
java.io.FileNotFoundException: music\sound.wav (The system cannot find the path specified)
[edit] The audio files are indeed packed into the .jar, but it still doesn’t work.
Is it even possible to play a sound from the windows prompt? If not, is there one that does?
Thanks,
SilverDuck
When the file is packaged into a jar file, it is no longer a File. It needs to be read as a resource. Try changing the code like this
InputStream inputStream = this.getClass().getResourceAsStream("music/sound.wav");
try (AudioInputStream _audio = AudioSystem.getAudioInputStream(inputStream)) {
Try either not packaging your music in jar (put it alongside) or load your packaged file as a resource.
See Java resource files for example.
Here Loading resources (images) contained in a .Jar file or in the classpath might be a better explanation.

No sound heard after playing an mp3 file

Hye there. I just want to play an mp3 file in my Java J2SE Desktop app. all is going fine with no exceptions but the problem is the audio sound is not heard I have checked my speakers as well use all kind a Java mp3 plugins like JMF and JLayer by JavaZoom. My code is listed below and i have added Jlayer.jar to my project also. Please help me give me solution of the problem because I'm trying since last 3 days but failed to do so! Please help me thanks in advance...
private void playMe(){
try{
String file="E://Net Beans Work Space//mp3//a.mp3";
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
}catch(Exception e){
System.out.print("ERROR "+e);
}
}
Did you try to start the player?
player.start();

Categories