- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个用java编写的纸牌游戏。我想每当我将鼠标悬停在卡片上时播放声音效果。但与此同时,卡片会“弹出”。
但是,当我尝试通过 run() 方法实现它时,它会变得滞后,也就是说,卡片不会像没有声音时那样快地弹出。
因此我创建了另一个名为 run(int Effect)
和 reloadLine(SourceDataLine line, int Effect)
的方法。
reloadLine(line,effect)
与 run()
类似,只是我删除了 drain()
和 close ()
位于末尾,并将其移至 run(int Effect)
。
下面是我的 SoundEffects.java 类:
package nusMonopolyDealGUI;
import javax.media.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
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.SourceDataLine;
import javax.swing.JOptionPane;
public class SoundEffects implements Runnable
{
private static final int EXTERNAL_BUFFER_SIZE = 128000;
private static int BUTTON_CLICK = 0;
private final int MOUSE_OVER_CARD = 1;
private final int MOUSE_CLICK_CARD = 2;
private static int activeSoundEffect;
private static SourceDataLine lineOverCard = null;
private static SourceDataLine lineClickCard = null;
private static ArrayList<SourceDataLine> sound;
private static ArrayList<String> soundEffects;
// CONSTRUCTOR //
public SoundEffects(){
soundEffects = new ArrayList<String>();
populateSoundEffects();
}
private void populateSoundEffects() {
try{
Scanner scanner = new Scanner(new File("soundEffectsList.txt"));
while(scanner.hasNextLine()){
String line = scanner.nextLine();
soundEffects.add(line);
}
scanner.close();
}
catch (IOException exp){
System.out.println("soundList.txt not found!");
}
//update soundEffects ArrayList with paths names of type D:\the\directory\path\...
for (int i = 0; i <soundEffects.size(); i ++){
String path = soundEffects.get(i);
URL pathURL = getClass().getResource("/music/" + path + ".wav");
String pathString = pathURL.toString();
String properPathString = pathString.replace("file:/", "");
soundEffects.set(i, properPathString);
}
//fill up the class attribute lines first for fast playback
reloadLine(lineOverCard, MOUSE_OVER_CARD);
reloadLine(lineClickCard, MOUSE_CLICK_CARD);
}
// METHODS //
public void setActiveSound(int i){
activeSoundEffect = i;
}
public void run(int effect){
switch(effect){
case MOUSE_OVER_CARD:
System.out.println("lineopen: "+ lineOverCard.isOpen());
if (!lineOverCard.isActive()){
lineOverCard.drain();
lineOverCard.close();
}
reloadLine(lineOverCard, MOUSE_OVER_CARD);
break;
case MOUSE_CLICK_CARD:
lineClickCard.drain();
lineClickCard.close();
reloadLine(lineClickCard, MOUSE_CLICK_CARD);
break;
}
}
//reload the line to reduce waiting time to load the line from buffer.
public void reloadLine(SourceDataLine line, int effect){
/*
* create an abstract object File to represent the directory of the .wav file.
*/
String filename = soundEffects.get(effect);
System.out.println("first time here");
File soundFile = new File(filename);
System.out.println(filename);
/* create an AudioInputStream and give it the .wav file
* @exception: dump the stack trace and exit the system.
*/
AudioInputStream audioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
/*
* get info on the .wav file
* this info is used by Java Sound to get a compatible Line
*/
AudioFormat audioFormat = audioInputStream.getFormat();
/*
* Create a SourceDataLine (used to generally play an audio file)
* Create an DataLine.Info object to be passed into the SourceDataLine
* so it will fetch the compatible line (getLine(info)) to use.
*/
//line = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try
{
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(audioFormat); //need to open a line before inputting audio input
}
catch (LineUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
line.start();
/*
* Line is ready to pass audio input.
* We write the audio data (.wav) into the line
* 1) read data from audioInputStream into a BUFFER
* 2) write from BUFFER to Line
* 3) we loop
* audioInputStream ---> BUFFER ---> Line
* until we reeach the end of audioInputStream
* indicated by a -1 from the read method of the audioInputStream (ie. audioInputStream.read(arg0, arg1, arg2))
*/
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
while (nBytesRead != -1)
{
try
{
nBytesRead = audioInputStream.read(abData, 0, abData.length);
}
catch (IOException e)
{
e.printStackTrace();
}
if (nBytesRead >= 0)
{
int nBytesWritten = line.write(abData, 0, nBytesRead);
}
}
}
public void run()
{
/*
* create an abstract object File to represent the directory of the .wav file.
*/
String filename = soundEffects.get(activeSoundEffect);
File soundFile = new File(filename);
/* create an AudioInputStream and give it the .wav file
* @exception: dump the stack trace and exit the system.
*/
AudioInputStream audioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
/*
* get info on the .wav file
* this info is used by Java Sound to get a compatible Line
*/
AudioFormat audioFormat = audioInputStream.getFormat();
/*
* Create a SourceDataLine (used to generally play an audio file)
* Create an DataLine.Info object to be passed into the SourceDataLine
* so it will fetch the compatible line (getLine(info)) to use.
*/
SourceDataLine line = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try
{
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(audioFormat); //need to open a line before inputting audio input
}
catch (LineUnavailableException e)
{
e.printStackTrace();
System.exit(1);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
line.start();
/*
* Line is ready to pass audio input.
* We write the audio data (.wav) into the line
* 1) read data from audioInputStream into a BUFFER
* 2) write from BUFFER to Line
* 3) we loop
* audioInputStream ---> BUFFER ---> Line
* until we reeach the end of audioInputStream
* indicated by a -1 from the read method of the audioInputStream (ie. audioInputStream.read(arg0, arg1, arg2))
*/
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
while (nBytesRead != -1)
{
try
{
nBytesRead = audioInputStream.read(abData, 0, abData.length);
}
catch (IOException e)
{
e.printStackTrace();
}
if (nBytesRead >= 0)
{
int nBytesWritten = line.write(abData, 0, nBytesRead);
}
}
/*
* after filling the line, we drain it
* ie. play the data in the line
*/
line.drain();
//close the line after playing.
line.close();
}
}
这个想法是为预加载 .wav 文件的类提供两个 SourceDataLine 属性。
问题是有轻微的滞后
最佳答案
我没有仔细看过你的代码,因为它真的很难阅读。你应该
但是,据我了解,您的方法比需要的更复杂。看看这里: More Advanced Audio Controls in Java
它并不能解释您的所有问题,但应该已经减少了相当多的代码。另外,这段代码应该运行得更快。因此,即使您不使用多线程工作,您的滞后问题也可能会这样消失。
关于java - 预加载 SourceDataLine 以减少延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4124358/
我正在阅读哈德利的 Advanced R并尝试一些东西。我正在尝试创建一个 lazy闭包函数返回一个带有提供的函数 data.frame在其环境中以及使用 with并且能够在以后提供额外的函数参数。
我有两个 ViewController。初始 ViewController 是输入和存储 URL 的地方。此初始 ViewController 的 viewDidLoad 还应该在应用程序启动时开始加
你是怎么用的 对于应用程序中的 ListView 项也应该在设备 <11 上运行? 由于 activated_state 在 HC 之前不可用,我只能想到两个肮脏的解决方法: 在您的 Activit
我正在为 android (2.1 > 3.1) 编写一个应用程序,我想使用熟悉的做法,即在 Honeycomb 应用程序中使用应用程序图标来进入家庭 Activity ,但是,当我之前运行该 Act
如果搜索的键不存在,我如何覆盖方法 GET 或编写一个将在服务器端执行的新函数返回另一个键值? 示例: 如果关键字“word_1 word_2 word_3 word_4”不存在则搜索关键字“word
对于我的存储库,我使用的是 Git 和 Stash。在 Stash 端,我限制了(只读)对 master 的访问权限,因此任何用户都可以从 master 分支分支以获取功能/分支,但不能直接 merg
如何配置dgrid及其存储以定义渲染行时是否已经选择了行? 例如,如果我的行数据是这样的: { id: 1, name: 'Item Name', selected: true } 我当前
有没有一种方法可以将变量从一个 BeanShell 前/后处理器引用到另一个 BeanShell 处理器(它们在同一个线程组中)? 如果我在 HTTP 请求下的 BeanShell 预处理器中创建了一
问题 我已尝试添加预操作 shell 脚本,这些脚本会根据我正在构建的内容打开/关闭我的 .pch 文件中的某些定义。 但是,在运行构建时,没有任何反应。我不是一个流利的 shell 脚本编写者,所以
我有一个 HTML 字符串用作 jQuery 输入文档。 // the variable html contains the HTML code jQuery( html ).find( 'p' ).
在 Mercurial 中允许 merge 之前有没有办法进行一些检查? 通过将以下内容添加到 ~/.hg/hgrc,我找到了更新前 Hook ,并拥有一个在允许更新之前运行的脚本: [hooks]
总结: 预 Controller Hook 是否在缓存期间执行?是否有任何 Hook 点可以执行? (系统前?) 我应该强调一个事实,即 Hook 不会影响发送到浏览器的内容。这不是问题。 详细版:
我正在使用适用于 android 的 Skobbler Map API,到目前为止它一直非常好。按照官方的“操作方法”,我已经能够将 map 应用到我的应用程序中。比我可以让应用程序下载 map 并离
当我安装bcrypt时我的 hapi js 项目的模块尚未安装,它显示类似 node-pre-gyp install --fallback-to-build 我尝试通过运行来安装; npm i nod
我试图使用此代码的变体: apply plugin: 'java' apply plugin: 'idea' idea.workspace.iws.withXml { provider ->
假设我们有一个 PHP 项目,其依赖项 A 和 B 分别依赖于 PHP 库 X,但版本不同。 通常,人们会使用诸如 composer 之类的 PHP 依赖管理器,它可以通过在与 A 和 B 兼容的版本
这似乎违背了代码块的目的,但我希望能够在代码块中加粗。例如,如果我想将返回行加粗: int main(void) { **return 0;** } 最佳答案 您必须在 HTML 中执行此操作
我们是否应该使用 Huggingface(预)训练一个 BERT 无框模型的小写输入数据?我查看了 Thomas Wolf ( https://github.com/huggingface/trans
我有两个模式: 技能: var mongoose = require("mongoose"); var SkillSchema = new mongoose.Schema({ skill: {
我这里有问题。这适用于 Chrome,但我无法在 IE11 的 index.html 中使用任何动画。当它不想工作时,我会看到一个静态屏幕。同样在 IE 中,消息不会像它应该的那样消失。如果我将 di
我是一名优秀的程序员,十分优秀!