Privacy-O
Privacy-O

Reputation: 53

Java - Multi threading sound clips to play at same time

Problem; Only hearing one sound clip when executed. After one sound has played the other doesn't play & neither can play at the same time.

Result; To be able to play 2 sounds at the same time.

Code:

 import java.io.*;
 import javax.sound.sampled.*;
 public class ThreadPlay extends Thread {
 private String filename; // The name of the file to play
 private boolean finished; // A flag showing that the thread has finished

private ThreadPlay(String fname) {
    filename = fname;
    finished = false;
}

public static void main(String[] args) {
    ThreadPlay s1 = new ThreadPlay("soundClip1.wav");
    ThreadPlay s2 = new ThreadPlay("soundClip2.wav");
    s1.start();
    s2.start();

    while (!s1.finished || !s2.finished);


    System.exit(0); // Java Sound bug fix...
}

public void run() {

    try {
        File file = new File(filename);
        AudioInputStream stream = AudioSystem.getAudioInputStream(file);
        AudioFormat format = stream.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, format);
        Clip clip = (Clip)AudioSystem.getLine(info);
        clip.open(stream);
        clip.start();
        Thread.sleep(100);
        while (clip.isRunning()) { Thread.sleep(100); }
        clip.close();
    }

    catch (Exception e) { }
    finished = true;
}
  }

Audio Lines:

 AudioSystem.getMixerInfo() results in:
                  [Ljavax.sound.sampled.Mixer$Info;@52cc8049

 Array length: 7 
 Contents:
     PulseAudio Mixer, version 0.02
     default [default], version 4.4.0-66-generic
     PCH [plughw:0,0], version 4.4.0-66-generic
     PCH [plughw:0,1], version 4.4.0-66-generic
     PCH [plughw:0,3], version 4.4.0-66-generic
     PCH [plughw:0,7], version 4.4.0-66-generic
     Port PCH [hw:0], version 4.4.0-66-generic

  For each mixer, AudioSystem.getMixer(AudioSystem.getMixerInfo()[x])
   Results:
      org.classpath.icedtea.pulseaudio.PulseAudioMixer@685f4c2e
      com.sun.media.sound.DirectAudioDevice@7a07c5b4
      com.sun.media.sound.DirectAudioDevice@5ce65a89
      com.sun.media.sound.DirectAudioDevice@1de0aca6
      com.sun.media.sound.DirectAudioDevice@443b7951
      com.sun.media.sound.DirectAudioDevice@45283ce2
      com.sun.media.sound.PortMixer@4d76f3f8

Upvotes: 2

Views: 2490

Answers (2)

Privacy-O
Privacy-O

Reputation: 53

Fixed:

  public void run() {
    try {
        File file = new File(filename);
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
        DataLine.Info info = new DataLine.Info(Clip.class, audioInputStream.getFormat());
        Clip clip = (Clip)AudioSystem.getLine(info);
        clip.open(audioInputStream);
        clip.start();
        clip.loop(Clip.LOOP_CONTINUOUSLY); // There are several different amounts of time you can loop it, so you can change this if you want, or you can just use clip.stop() whenever you want.
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    finished = true;
}

Also, if you're running Ubuntu like myself you need to remove OpenJDK/OpenJRE and set SunJDK/SunJRE as Default.

Upvotes: 0

Anonymous
Anonymous

Reputation: 179

Imports:

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

Run method:

public void run() {
    try {
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(this.getClass().getResource("NameOfFile.wav"));
        Clip clip = AudioSystem.getClip();
        clip.open(audioInputStream);
        clip.start();
        clip.loop(Clip.LOOP_CONTINUOUSLY); // There are several different amounts of time you can loop it, so you can change this if you want, or you can just use clip.stop() whenever you want.
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

If you use this and piece of code over multiple threads, it should work. If I am correct in assuming that you are initiating this piece of code twice, once for each thread, then this should work. I hope that this helps.

Upvotes: 3

Related Questions