I play sound effects (WAV files) in a game with javax.sound.sampled.Clip. However, the effect that has 2 seconds is not played entirely, only a part of it. If I add clip.loop(2) then the sound is played multiple times correctly, i.e. the whole 2 seconds. What is wrong and how to fix it?
package com.zetcode;
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JOptionPane;
public enum SoundEffect {
EXPLODE("resources/explosion.wav"),
HITGROUND("resources/impact.wav"),
CANNON("resources/cannon.wav");
private Clip clip;
SoundEffect(String soundFileName) {
try {
URL url = this.getClass().getClassLoader().getResource(soundFileName);
try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url)) {
clip = AudioSystem.getClip();
clip.open(audioInputStream);
}
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
JOptionPane.showMessageDialog(null, "Cannot play sound", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
public void play() {
if (clip.isRunning()) {
clip.stop();
}
clip.setFramePosition(0);
clip.start();
}
static void init() {
values();
}
}
Are you aware and accounting for the fact that when a clip is played (via start() method), the thread playing the code is a daemon? Unlike regular threads, a daemon thread will not prevent a Java program from closing. If your program ends before the sound has finished executing, the daemon status thread also ends even if it is not finished.
Are you trying to run the sample code SoundClipTest or SoundEffectDemo? It looks to me like SoundClipTest is going to exit as soon as the program executes the start() method, whereas the SoundEffectDemo will persist and allow the sound to finish playing.
** Edit 1 **
Am just now noticing that you have made your SoundEffect an enum. I've never seen that approach used before.
One approach I've used is to create a class called GameSound, and have its constructor preload each Clip individually and hold them in memory, ready for playback. Then, I create a method or methods for their playback.
gameSound.playExplosion();
The GameSound class would have instance variables for each sound effect.
private Clip explosion;
private Clip cannon;
And GameSound constructor would have:
URL url = this.getClass().getResource("resources/explosion.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
explosion = AudioSystem.getClip();
explosion.open(ais);
and GameSound would have method:
public void playExplosion()
{
if (explosion.isRunning()) {
explosion.stop();
}
explosion.setFramePosition(0);
explosion.start();
}
If you prefer to have the play method take an argument and use that on a switch structure to select which sound to play, that is fine, too, especially as it eliminates some code duplication.
This approach might work better (easy to add to GameSound as code gets more complex), and shouldn't be too difficult to code. I'd be curious to learn whether or not changing to this plan makes the problem go away or not.
** Edit 2 **
Sorry for mistakes in the above code. I fixed them when I made the following example. One thought, though, when I went to listen to the sound effects you linked, several of the explosions were kind of truncated to begin with. Have you played the sounds in another app (like Windows Groove Music, or from the website where you got them) and verified that they are different when you play them in your code?
Here is a quickie "GameSound". I downloaded the Explosion+2.wav file from your reference as it was longer than the others and has a clear ending, and am referencing that.
package stackoverflow.janbodnar;
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class GameSound {
private final Clip explosion;
public GameSound() throws LineUnavailableException,
IOException, UnsupportedAudioFileException
{
URL url = this.getClass().getResource(
"resources/Explosion+2.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
explosion = AudioSystem.getClip();
explosion.open(ais);
}
public void playExplosion()
{
if (explosion.isRunning()) {
explosion.stop();
}
explosion.setFramePosition(0);
explosion.start();
}
}
And here is a simple Swing button that calls GameSound.
package stackoverflow.janbodnar;
import java.io.IOException;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class GameSoundTest extends JFrame
{
private static final long serialVersionUID = 1L;
private final GameSound gameSound;
public GameSoundTest() throws LineUnavailableException,
IOException, UnsupportedAudioFileException
{
gameSound = new GameSound();
JButton button = new JButton("Play Explosion");
button.addActionListener(e -> gameSound.playExplosion());
getContentPane().add(button);
}
private static void createAndShowGUI()
{
JFrame frame;
try
{
frame = new GameSoundTest();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.setBounds(0, 0, 200, 200);
frame.setVisible(true);
}
catch (LineUnavailableException | IOException
| UnsupportedAudioFileException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(() -> createAndShowGUI());
}
}
This all assumes, of course, that there is a subfolder called "resources" with the explosion SFX in it.
Related
thanks so much in advance for helping me with this seemingly tiny thing - yet I can't figure it out. MP4 Video/audio playback works just fine, yet I can't set the position in the video.
Here's my stripped down code:
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import javax.swing.JPanel;
import com.sun.jna.NativeLibrary;
import java.util.logging.Level;
import java.util.logging.Logger;
import uk.co.caprica.vlcj.binding.RuntimeUtil;
import uk.co.caprica.vlcj.player.base.ControlsApi;
import uk.co.caprica.vlcj.player.base.MediaApi;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.component.CallbackMediaPlayerComponent;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.component.callback.FilledCallbackImagePainter;
import uk.co.caprica.vlcj.player.component.callback.FixedCallbackImagePainter;
import uk.co.caprica.vlcj.player.component.callback.ScaledCallbackImagePainter;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.player.renderer.RendererItem;
import uk.co.caprica.vlcjplayer.event.TickEvent;
import uk.co.caprica.vlcjplayer.view.action.mediaplayer.MediaPlayerActions;
public class TestClass extends JPanel {
private EmbeddedMediaPlayerComponent ourMediaPlayer;
TestClass(){
//NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");
ourMediaPlayer = new EmbeddedMediaPlayerComponent();
/* Set the canvas */
Canvas c = new Canvas();
c.setBackground(Color.black);
c.setVisible(true);
/* Set the layout */
this.setLayout(new BorderLayout());
/* Add the canvas */
this.add(c, BorderLayout.CENTER);
this.setVisible(true);
this.add(ourMediaPlayer);
}
public void play() {
/* Play the video */
System.out.println("Starting...");
ourMediaPlayer.mediaPlayer().controls().setPosition((float) 0.5); // NOPE
ourMediaPlayer.mediaPlayer().media().play("/home/manfred/ExtraDisk/Work/BTL/Movement2022/walking.mp4"); // works
ourMediaPlayer.mediaPlayer().controls().stop(); // works
ourMediaPlayer.mediaPlayer().controls().setPosition((float) 0.5); //NOPE
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(TestClass.class.getName()).log(Level.SEVERE, null, ex);
}
ourMediaPlayer.mediaPlayer().controls().setPosition((float) 0.5); //NOPE
ourMediaPlayer.mediaPlayer().controls().setTime(2000); // NOPE
ourMediaPlayer.mediaPlayer().controls().start(); //works
//System.time.sleep(2);
System.out.println("Started!");
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(TestClass.class.getName()).log(Level.SEVERE, null, ex);
}
ourMediaPlayer.mediaPlayer().controls().stop(); // works
}
}
Playback via .mediaPlayer().media().play() works, so does start and stop via .mediaPlayer().controls().start() and .mediaPlayer().controls().stop().
What doesn't work is .mediaPlayer().controls().setTime(xx) and .mediaPlayer().controls().setPosition(xx), basically nothing happens.
What am I not doing right here? Is this a threading issue? Anyone have any working minimal examples?
Thanks again, any help is greatly appreciated!
It is not possible to use the API to set the time/position before playback has started.
LibVLC operates asynchronously for many operations. Just calling play() does not mean that playback has started, so setting the time/position immediately after play() is called will not (always) work.
There are at least two approaches you can use:
Wait for a media player "ready" event, and set the time/position (this will fire an event each time the media player is ready, so each time you play it, although you can write a one-shot listener that unregisters itself if you only want to do it the first time you play).
public static void main(String[] args) throws Exception {
MediaPlayer mediaPlayer = new MediaPlayerFactory().mediaPlayers().newMediaPlayer();
mediaPlayer.events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
#Override
public void mediaPlayerReady(MediaPlayer mediaPlayer) {
mediaPlayer.controls().setTime(10000);
}
});
mediaPlayer.media().play("/home/movies/whatever.mp4");
Thread.currentThread().join();
}
With this first approach there is the small risk that you will see one or two video frames rendered before skipping occurs.
Use media options to set the start time (in seconds, including fractions of seconds like 10.5):
public static void main(String[] args) throws Exception {
MediaPlayer mediaPlayer = new MediaPlayerFactory().mediaPlayers().newMediaPlayer();
mediaPlayer.media().play("/home/movies/whatever.mp4", ":start-time=10");
Thread.currentThread().join();
}
Thanks to caprica's ingenious insights, this snippet actually works (don't know why, but it does - and that's all that matters for now):
ourMediaPlayer.mediaPlayer().media().play("/home/manfred/ExtraDisk/Work/BTL/Movement2022/walking.mp4"); // works
ourMediaPlayer.mediaPlayer().controls().stop(); // works
ourMediaPlayer.mediaPlayer().controls().start(); // works
ourMediaPlayer.mediaPlayer().controls().setTime(5000); // WORKS
Still a bit of a mystery, but I'll take it!
After some days of research on the internet, I came here looking for help. I'm currently developing a short 2D game for friends (and really just for fun), and I learned about Clips some days ago. In this game, the player can gather objects (almost like the coins in Mario). My problem is that I have a very short sound (~ 1 sec for 50kB) played when gathering a coin, and if the player gather let's say 3 coins in 1 second, then the Clip make the game lag. If the precedent Clip has ended, then there is no lag. But if the precedent Clip has not ended, then trying to play the Clip again make the game lag (very much).
I have a very small computer, not very powerful, but this problem is really annoying. I get the exact same problem with a sound when the player throws a weapon. I have a short clip, and if the player throws this weapon too fast, the game lag ...
Here is the things I already tried :
Use an array of clips (of this same sound), and play a clip that is not currently playing
Use different clips (of this same sound), and same as before
Make multiple copies (of this same sound), and load it in different clips
Make a separate thread for playing sounds, but I'm not comfortable at all with threads :/
But none of these change this problem ...
Now here is part of my code. First is the class that I use to load a sound into Clip.
package sound;
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
// My own package
import utils.Utils;
public class SoundLoader {
public static Clip loadSound(String path) {
Utils.log("Loading " + path + " ... ");
try {
URL url = SoundLoader.class.getResource(path);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
AudioFormat format = audioIn.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip)AudioSystem.getLine(info);
clip.open(audioIn);
Utils.log("success\n");
return clip;
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
e.printStackTrace();
Utils.log("failed !\n");
return null;
}
}
}
Now is the class where I'll manage all sounds :
package sound;
import javax.sound.sampled.Clip;
// My own package
import principal.Handler;
public class SoundManager {
private Clip woosh;
private Clip coin1;
private Clip coin2;
public SoundManager(Handler handler) {
woosh = SoundLoader.loadSound("/resources/sounds/woosh2.wav");
coin1 = SoundLoader.loadSound("/resources/sounds/coin1.wav");
coin2 = SoundLoader.loadSound("/resources/sounds/coin2.wav");
}
public void wooshClip() {
startClip(woosh);
}
public void coin1Clip() {
startClip(coin1);
}
public void coin2Clip() {
startClip(coin2);
}
public synchronized void startClip(Clip clip) {
clip.stop();
clip.setFramePosition(0);
clip.start();
}
public void loopClip(Clip clip) {
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
public void stopClip(Clip clip) {
clip.stop();
}
}
And finally, when I want to play a Clip, I use the public method startClip() from my "Player" class (representing obviously the player).
handler.getSoundManager().wooshClip();
As I see my code, if the Clip is already playing, then the method starClip() should just stop it and replay it. Why is it lagging so much ? And why is it lagging even if I use different Clips ? What does make my game lag if I'm just stopping a Clip and replay it ?
I hope you will be able to help me ... It's rare that I don't find an answer on the web. If you need anything more to answer me just let me know !
Thanks for your time spent on this long question !
EDIT : I tried my game on a much powerful computer and there is absolutely no lag because of the audio clips ... But my question remains ! How could a 50kB clip make so much lag ?? I really don't understand the problem ...
Solution
I just discovered a solution while working on this project : now my entities that should play a sound when gathered by the player (like the sound of a coin in Mario) contain the audio clip of their "death". I don't understand exactly why it works now but it does ...
Use javafx.scene.media.AudioClip from JavaFX instead of javax.sound.sampled.Clip. AudioClip has lower latency.
Music works just fine when i click on the box, but music won't stop when I click on it again. Then if i click on the unchecked box again, the music plays again so 2 times at once! Please help me with stopping the music!
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {
InputStream inputStream = getClass().getResourceAsStream("panda - desiigner (donald trump remix).au");
AudioStream audioStream = null;
try {
audioStream = new AudioStream(inputStream);
} catch (IOException ex) {
Logger.getLogger(kekFrame.class.getName()).log(Level.SEVERE, null, ex);
}
int check;
if(jCheckBox1.isSelected() == true){
check = 1;
} else {
check = 0;
}
switch (check) {
case 1 : AudioPlayer.player.start(audioStream);
System.out.println("Music has started playing");
break;
case 0 : AudioPlayer.player.stop(audioStream);
System.out.println("Music has stopped playing");
break;
}
}
/**
* #param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JCheckBox jCheckBox1;
Assuming you have properly created a Clip, and can access both it and a boolean (isSelected, say) that reflects the state of the JCheckBox, the following simple code should work:
if (isSelected)
{
clip.setFramePosition(0);
clip.start();
}
else
{
clip.stop();
}
This can be included in the JCheckBox's ActionListener.
More information about using Clips can be found on the Java Tutorial's "Audio Thread" with specifics about the Clips here. There are clearer examples elsewhere if you do a search on how to use a Java Clip. The official Java Tutorial for audio emphasizes background and high-level concepts at the expense of practical examples, imho, which makes for difficult reading for those of us who are just starting out.
sun.audio.AudioPlayer is no longer supported! Even if it works on your PC, there is no guarantee it will work on other systems. Unfortunately, this is a situation where obsolete code examples live on in blogs and unofficial tutorials, which can happen a lot as languages evolve and the parties that wrote the tutorials don't update or maintain their posts.
In response to the OP's request, here is an example adapted from one of my JavaFX gui's. I don't use Swing anymore and am not keen to go back.
In the code where the JavFX Button is being built:
btnPlay = new Button("Play");
btnPlay.setOnAction(e -> handlePlay(e));
This calls the following method:
private void handlePlay(ActionEvent e)
{
if (!isPlaying)
{
clip.setFramePosition(0);
clip.start();
((Button)e.getSource()).setText("STOP");
isPlaying = true;
}
else
{
clip.stop();
((Button)e.getSource()).setText("PLAY");
isPlaying = false;
}
}
In this code, isPlaying is an instance variable that in this case merely tells us whether the button is on or off. The clip may very well play to its end and stop on its own while the button is still in a "playing" state. It would require wiring up a LineListener to have the button toggle back when the clip finishes playing.
Maybe you can adapt the above into something useful? It seems to me the choice of JCheckBox is dubious, and a JToggleButton might be a better choice. Examples of writing Listeners for Swing Buttons can be found here.
I have written this code for playing audio file, I want to get indication when my audio file ends after playing. I have tried AS.getMicrosecondLength() == AS.getMicrosecondPosition() but these methods are undefined for the AudioStream. Please tell how I can do that.
import java.io.FileInputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class A {
public static void main(String arg[]) throws Exception {
AudioStream AS = new AudioStream(new FileInputStream("sounds.wav"));
AudioPlayer.player.start(AS);
}
}
AS.getMicrosecondLength() == AS.getMicrosecondPosition() could be used to set off the flag when the clip has ended
I've got a little problem. I'm playing mp3 using Java sound sampled and I want to stop playing when I click the button. So I came up with something like this:
package sk.umb.osadnici.Client.Core.getterImages;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine.Info;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.FloatControl;
import javazoom.jl.player.advanced.AdvancedPlayer;
import static javax.sound.sampled.AudioSystem.getAudioInputStream;
import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;
public class GetterForBGMusic {
private SourceDataLine line;
private URL url;
public URL bgUrl, bgUUUrl;
private boolean canPlay = true;
public void runMusic() {
final GetterForBGMusic player = new GetterForBGMusic();
player.play();
}
public void play() {
URL inTTT = getClass().getResource("../sounds/bgMusic.mp3");
try (AudioInputStream in = getAudioInputStream(inTTT)) {
AudioFormat outFormat = getOutFormat(in.getFormat());
Info info = new Info(SourceDataLine.class, outFormat);
try (SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info)) {
if (line != null) {
System.out.println(canPlay);
line.open(outFormat);
line.start();
stream(getAudioInputStream(outFormat, in), line);
line.drain();
line.stop();
}
}
} catch (UnsupportedAudioFileException
| LineUnavailableException
| IOException e) {
throw new IllegalStateException(e);
}
}
private AudioFormat getOutFormat(AudioFormat inFormat) {
final int ch = inFormat.getChannels();
final float rate = inFormat.getSampleRate();
return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
}
private void stream(AudioInputStream in, SourceDataLine line)
throws IOException {
while (true) {
System.out.println(this.getCanPlay());
}
}
public void setCanPlay(boolean play) {
this.canPlay = play;
}
public boolean getCanPlay() {
return canPlay;
}
private void booleanValue() {
while (true)
System.out.println(canPlay);
}
}
Im using this code, if i call booleanValue method in constructor, everything is fine. but if call this method inside stream there is no change after value change.
Or can someone tell me how to stop this: http://odoepner.wordpress.com/2013/07/19/play-mp3-or-ogg-using-javax-sound-sampled-mp3spi-vorbisspi/
Your program is single-threaded, which means that it executes the sequence of "commands" you programmed from top to bottom.
For example, in this example
setCanPlay(true);
play(); //your for loop
setCanPlay(false);
the setCanPlay(false) instruction will only execute once the for loop has finished executing.
What you need is to have the for loop running in the background, and to be able to modify canPlay while the for loop is running. That's called multi-threading and you should lookup the classes Runnable, Task and Service in the java api doc to learn how to implement it.
You would end up with something like this:
setCanPlay(true);
play(); //your for loop, launched in another thread.
setCanPlay(false); // Executed while the for loop is running
That would start and end the playing instantly.
Multithreading is the only way to stop an executing program (from the outside).