Java-多线程声音片段可同时播放 [英] Java - Multi threading sound clips to play at same time

查看:91
本文介绍了Java-多线程声音片段可同时播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题;执行时仅听到一个声音片段.一种声音播放完后,另一种则不播放.两者都不能同时播放.

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.

结果;为了能够同时播放2种声音.

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

代码:

 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;
}
  }

音频线路:

 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

推荐答案

导入:

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

运行方法:

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.

这篇关于Java-多线程声音片段可同时播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆