I'm using jLayer 1.0 to play a simple mp3 file. It works fine apart from the first 3 seconds or so (adding 5 seconds of silence to the start of the track fixes the issue). This is the code I'm using:
try
{
final Player player = new Player(getClass().getResourceAsStream("1.mp3"));
new Thread()
{
public void run()
{
try
{
player.play();
}
catch (Exception e) { System.out.println(e); }
}
}.start();
}
catch (Exception e)
{
System.out.println("Something went wrong!");
System.out.println(e);
}
I'm probably doing something stupid (I'm pretty sure I don't need to put the thread in the try block anyway) but it does play apart from the distortion.
Audio sample of distortion: http://www.filedropper.com/broken
Original track: http://www.filedropper.com/dennyschneidemessermodernwarstory
Music by: denny schneidemesser (by-nc-nd)
Any idea of what is is going on?
Issue completely fixed after updating to v1.0.1.
The version hosted on this page:
http://introcs.cs.princeton.edu/java/faq/mp3/mp3.html
Is outdated.
Related
I'm trying to make the program wait for the user to click on the GUI.
But Thread.sleep() just sleeps the entire GUI. So all I can see is an empty window.
code:
while (!enteredField) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Swing events need to happen on the Event Dispatch Thread.
Have you tried
SwingUtilities.invokeAndWait(() -> Thread.sleep(20))
https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingUtilities.html#invokeAndWait-- ?
I have had problem like this before. so you are not alone. I made mistakes like this.
Your Problem
while (!enteredField) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Solution
Thread thread = new Thread(() -> {
while (!enteredField) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
* Detailed Response*
' What you did was that you created a thread inside a while loop. Basically You paused the whole GUI for 10 milliseconds, the whole program for matter of fact. Furthermore, I do not understand what your objective is. You said that you want to wait for the user to click on the GUI.Suggestions would be, listen for the button click. for example Button.setOnAction(() -> {}); That would do he job instead of sleeping. I just the only case that you would want to pause or sleep is if you are animating somethings.
When I try to call Desktop.getDesktop().open(), my program crashes.
I'm on Ubuntu GNOME 16.10, running Gnome 3.20.4. I haven't had a chance to test this code on another platform, but on this platform it is definitely crashing the program.
browseMenuItem.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Platform.runLater( new Runnable() {
public void run() {
try {
System.out.println ( Desktop.isDesktopSupported() );
Desktop.getDesktop().open( albumTable.getSelectionModel().getSelectedItem().getPath().toFile() );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
});
Any thoughts on how to fix or troubleshoot this?
You're mixing JavaFX and AWT, which is likely causing the problem. Instead of
Desktop.getDesktop().open(file);
try using the JavaFX API:
getHostServices().showDocument(file.toURI().toString());
getHostServices() is defined in Application (so you may need to retrieve it in your start method and pass it to whichever object - perhaps a controller - is registering the listener with the menu item).
It looks like #James_D's answer is the better way to do things, but currently (as of 2017/05/03) it doesn't work on the OpenJDK / OpenJFK.
Thankfully, his comment about mixing JFX and Swing helped me find a solution which will work on OpenJDK / OpenJFX:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Desktop.getDesktop().open( albumTable.getSelectionModel().getSelectedItem().getPath().toFile() );
} catch (IOException e) {
e.printStackTrace();
}
}
});
The trick was using SwingUtilities.invokeLater() rather than Platform.runLater(), since the code inside is swing.
I don't have any code to show in particular to link because this is quite a general question...you see, I made a small game applet with 8 AudioClip variables, and these were supposed to play every time a certain action was taken by the user. Ex: Player clicks = Shooting sound plays. The thing is, the moment I added these AudioClip files my program just freezes terribly and becomes borderline unplayable if the freezing is particularly bad.
The simple way I did it is here (From the book, actually)
AudioClip playerShooting;
playerShooting=getAudioClip(getDocumentBase(),"PlayerShooting.wav");
Then I would just use the following whenever the user clicked:
playerShooting.play():
My program worked smoothly before I added these wav files. They aren't even that long. Where did I go wrong here, and, how can I remove the random freezing?
EDIT:
This is the code I am running:
public void mouseClicked(MouseEvent e)
{
if(playerAlive)
{
timeShotsAfter=System.currentTimeMillis();
if((timeShotsAfter-timeShotsBefore)>=100)
{
if(shotIndex<10)
{
playerShooting(); //Here is where I call the function
shotFiredX[shotIndex]=currentX;
shotFiredY[shotIndex]=currentY;
shotSize[shotIndex]=20;
}
if(shotIndex<10)
shotIndex++;
else
shotIndex=0;
timeShotsBefore=System.currentTimeMillis();
}
else{}
toggleShooting=false;
}
}
This is the function:
public void playerShooting()
{
new Thread(
new Runnable() {
public void run() {
try {
playerShooting.play();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
I was having the same problem a while back. What I did to solve my problem was update to JDK 8. In previous JDK versions, audio files appear to have been an afterthought and can be buggy. If you are still having problems, JDK 8 has the ability to play mp3 files which are significantly smaller than wav (you may want to try this). Make sure you use sound.stop() when your clips are done as this might free up some memory.
Play the audio clip in another thread?
EDIT:
new Thread(
new Runnable() {
public void run() {
try {
playerShooting.play():
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
Edit:
I'm not quite sure if this part is correct:
getAudioClip(getDocumentBase(),"PlayerShooting.wav");
Try adding System.out.println(getDocumentBase()); to see whether the location is correct.
I use Clips in a game. The clips play fine, but after some "shots", the following problem occurs
Exception in thread "PulseAudio Eventloop Thread" java.lang.IllegalStateException: drain failed
at org.classpath.icedtea.pulseaudio.EventLoop.native_iterate(Native Method)
at org.classpath.icedtea.pulseaudio.EventLoop.run(EventLoop.java:133)
at java.lang.Thread.run(Thread.java:724)
My code:
public static Clip[] sounds;
...
sounds = new Clip[3];
sounds[0] = getClip("gun.wav");
sounds[1] = getClip("click.wav");
sounds[2] = getClip("over.wav");
...
private void playSound(Clip clp) {
final Clip clip = clp;
Runnable soundPlayer = new Runnable() {
#Override
public void run() {
try {
if(clip.isActive() || clip.isRunning()) clip.stop();
clip.setMicrosecondPosition(0);
clip.start();
} catch (Exception e) {
e.printStackTrace();
}
}
};
new Thread(soundPlayer).start();
}
public void shoot() { //runs when space is clicked
if(canShoot) playSound(sounds[0]);
}
So I had a similar problem on OS X, where the clip was sometimes not playing if you tried to stop it and restart it from the beginning. I fixed it by calling flush() right after stop():
if(clip.isActive() || clip.isRunning()) {
clip.stop();
clip.flush();
}
You shouldn't need to spawn a background thread to interact with the Clip. The most commonly used methods of Clip, like "start" and "stop" operate asynchronously, meaning they do not block, so it should be okay to call them from the GUI/AWT/Swing thread.
This page has some reasonable examples:
http://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html
I see you are using IcedTea and PulseAudio. You may experience different behavior when using javax.sound in this JVM, as opposed to the Oracle JVM, since the implementations of javax.sound are significantly different between these two products.
I am using freeTTS library for converting text to speech. I am able to program my code using this library where i can play the speech for a particular text using following code:
Voice voice = VoiceManager.getInstance().getVoice("kevin16");
if (voice != null) {
voice.allocate();
}
voice.speak("Hello world");
Is there a way using which i can get a callback when the tts lib has completed the speak process?
I found the answer myself.. we don't need a callback when the lib has completed the speak process. the control goes on the next line only when speak process ends.
that's how i did it:
Thread t = new Thread() {
#Override
public void run() {
super.run();
try {
voice = initializeTTS(); // a func to initialize TTS lib.
voice.speak("Hello world");
// do whatever you want to do from here only.
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();