为什么这个code不播放声音文件 [英] why this code doesn't play the sound file

查看:148
本文介绍了为什么这个code不播放声音文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的code

import javax.sound.sampled.*;
import java.io.*;

public class Tester {
static Thread th;


public static void main(String[] args) {
    startNewThread();   
   while( th.isAlive() == true) {
       System.out.println("sound thread is working");
   }
}

public static void startNewThread() {
   Runnable r = new Runnable() {
       public void run() {
           startPlaying();
       }
   };
   th =new Thread(r);
   th.start();
} 
public static void startPlaying() {
   try {            
        AudioInputStream ais = AudioSystem.getAudioInputStream(new File("d:/UnderTest/wavtester.wav"));
        Clip clip = AudioSystem.getClip();
        clip.open(ais);
        clip.loop(-1); // keep playing the sound                  
   } catch(Exception exc) {
       System.out.println(exc);
     }       
 }
}

这code确实给输出的声音线程工作,但不玩什么。在这个code我已经开始一个单独的线程用于播放声音和程序不应该终止,直到声音线程完成它的工作。但印花系列的声音线程的工作后,该程序将终止

This code does give the output sound thread working , but does not play anything. In this code i have started a separate thread for playing sound and the program should not terminate till the sound thread has finished it's job .But the program terminates after printing series of sound thread working.

,这是什么原因的(该程序终止,不播放声音)的?

推荐答案

的问题是,剪辑已启动后台 后台螺纹的播放波形文件。

The problem is that Clip already launches a background daemon thread to play the wave file.

所以,你的code的执行流程如下:

So, the execution flow of your code is as follows:


  • 在主线程启动二级(没用)螺纹

  • 辅助线程启动一个守护线程(将播放的声音)

  • 在同时,主线程保持打印东西,而辅助线程是活

  • 当辅助线程完成的启动播放线程,它就会结束,所以辅助线程将不处于活动状态了

  • 主线程会注意到辅助线程是不活动的结束,以及

  • 由于播放线程是的守护线程的,JVM将退出(因为留下的唯一线程是守护线程)

  • the main thread launches a secondary (useless) thread
  • the secondary thread launches a daemon thread (which will play the sound)
  • in the mean time, the main thread keeps printing something while the secondary thread is alive
  • When the secondary thread finishes launching the playback thread, it will end, so the secondary thread will not be in the active state anymore
  • the main thread will notice that the secondary thread is not active and will end as well
  • Since the playback thread is a daemon thread, the JVM will exit (because the only threads left are daemon threads)

最后的结果是precisely你所看到的:当辅助线程正在启​​动播放线程一些文本得到由主线程打印,而当播放线程将开始播放,狂飙的JVM完成。有时候,你甚至可以收听从耳机的一些点击的JVM退出之前(如声音开始播放)。

The final result is precisely what you see: some text get printed by the main thread while the secondary thread is launching the playback thread, and when the playback thread would start playing, boom, the JVM finishes. Sometimes you might even be able to listen some "clicking" from the headphones (as the sound starts to play) before the JVM exits.

最简单的解决方法是使辅助线程(即非守护线程),而播放声音时睡着了。

The easiest fix is to make the secondary thread (ie, a non-daemon thread) sleep while the sound is playing.

...
  clip.open(ais);
  clip.loop(-1);
  Thread.sleep(amountToSleep);
...

一个重要的事情要注意:大约一年前,当我与Java的API工作,我注意到,该方法 getMicrosecondLength()是马车。我被编码在Windows和Linux操作系统,在一个平台上我能得到正确的值,但在其他的,同样的方法将返回以毫秒为单位的长度!

One important thing to notice: about 1 year ago, when I worked with that java API, I noticed that the method getMicrosecondLength() is buggy. I was coding both in Windows and Linux, and in one platform I'd get the correct value, but in the other, the very same method would return the length in milliseconds!

我发现,最可靠的方式来获得声音的实际长度是使用 getFrameLength()方法,并计算其长度。

I found that the most reliable way to get the real length of the sound is to use the getFrameLength() method, and calculate the length from that.

我无法找到code我回了信,然后在这个笔记本。我将在稍后为您在另一台电脑,如果我找到它,我将发布一个完整的示例(即可靠地工作在Windows与Sun JVM和Linux与OpenJDK的两种或Sun)。

I couldn't locate the code I wrote back then in this notebook. I will check later in another PC, and if I find it I will post a complete example (that works reliably on both Windows with Sun JVM and Linux with either OpenJDK or Sun).

这篇关于为什么这个code不播放声音文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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