栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

音频文件在Eclipse IDE中播放;但不是JAR文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

音频文件在Eclipse IDE中播放;但不是JAR文件

Adding

clip.drain()

after
clip.start()
seems to have worked okay for me (IDE and command line
both with and without
&
)

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;public class Sandbox {    public static void main(String[] args)  {        try { URL url = Sandbox.class.getResource("/sound-effects/Loud-alarm-clock-sound.wav"); AudioInputStream ais = AudioSystem.getAudioInputStream(url); AudioFormat af = ais.getFormat(); DataLine.Info info = new DataLine.Info(Clip.class, af); Clip clip = (Clip) AudioSystem.getLine(info); clip.open(ais); clip.start(); System.out.println("Drain..."); clip.drain(); System.out.println("...Drained");        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException exp) { exp.printStackTrace();        }    }}

Now, having said that, I have found

drain
a little unreliable in the past,
especially when there are multiple sounds playing in which case I tend to use
a
LineListener

For example…

import java.io.IOException;import java.net.URL;import java.util.logging.Level;import java.util.logging.Logger;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.LineEvent;import javax.sound.sampled.LineListener;import javax.sound.sampled.LineUnavailableException;import javax.sound.sampled.UnsupportedAudioFileException;public class Sandbox {    protected static final Object LOCK = new Object();    public static void main(String[] args) {        try { URL url = Sandbox.class.getResource("/sound-effects/Loud-alarm-clock-sound.wav"); AudioInputStream ais = AudioSystem.getAudioInputStream(url); AudioFormat af = ais.getFormat(); DataLine.Info info = new DataLine.Info(Clip.class, af); Clip clip = (Clip) AudioSystem.getLine(info); clip.open(ais); clip.addLineListener(new LineListener() {     @Override     public void update(LineEvent event) {         System.out.println(event.getType());         if (event.getType() == LineEvent.Type.STOP) {  synchronized (LOCK) {      LOCK.notify();  }         }     } }); clip.start(); synchronized (LOCK) {     LOCK.wait(); }        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException | InterruptedException exp) { exp.printStackTrace();        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438425.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号