在我的游戏中有 .wav、.au 声音后极度滞后? [英] extreme lag after having .wav, .au sound in my game?

查看:22
本文介绍了在我的游戏中有 .wav、.au 声音后极度滞后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在网上搜索资源非常有限.在操作执行方法中:

i tried to search up online there are very limited resources. in the action Performed Method:

actionPerformed(){
---------------
new Sound();}

和声音类

 public Sound(){
   try {
    String filePath="path\\file.wav";
    File path= new File(filePath);
    AudioInputStream stream;
    AudioFormat format;
    DataLine.Info info;
    Clip clip;

    stream = AudioSystem.getAudioInputStream(path);
    format = stream.getFormat();//becomes formatted
    info = new DataLine.Info(Clip.class, format);// info
    clip = (Clip) AudioSystem.getLine(info);// casting for clip
    clip.open(stream); // clip opens stream with path
    clip.start();
}
    catch (Exception e) {
        System.out.println("errors");
    }

}

谢谢.

推荐答案

根据.start() 方法:

允许一条线参与数据 I/O.如果在一行上调用已经运行,这个方法什么都不做.除非数据在缓冲区已被刷新,该行从第一个开始恢复 I/O线停止时未处理的帧.什么时候音频捕获或播放开始,生成一个 START 事件.

Allows a line to engage in data I/O. If invoked on a line that is already running, this method does nothing. Unless the data in the buffer has been flushed, the line resumes I/O starting with the first frame that was unprocessed at the time the line was stopped. When audio capture or playback starts, a START event is generated.

没有提到以异步方式播放的音频.这意味着您的声音将由调用 start 方法的线程处理,因为您在事件处理程序中调用它,最终将意味着您的 Event Dispatching Thread负责播放音频.

There is no mention of the audio being played in an asynchronous manner. This means that your sound will be tackled by the thread which calls the start method, which since you are calling it in your event handler, will eventually mean that your Event Dispatching Thread is taking care of playing audio.

该线程还负责在屏幕上绘制内容,因此在其上运行的任何其他内容(在您的情况下为音频)都会对您的渲染产生负面影响.

This thread is also responsible for drawing stuff on screen, so any other thing that runs on it, audio in your case, will have a negative effect on your rendering.

您可以使用如下代码(取自此处)来制作您的音频在单独的线程上播放.这应该可以解决您的滞后问题.

You could use some code like below (taken from here) to make your audio be played on a separate thread. This should cater for your lagging problems.

import java.io.File; 
import java.io.IOException; 
import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.DataLine; 
import javax.sound.sampled.FloatControl; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.SourceDataLine; 
import javax.sound.sampled.UnsupportedAudioFileException; 

public class AePlayWave extends Thread { 

    private String filename;

    private Position curPosition;

    private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb 

    enum Position { 
        LEFT, RIGHT, NORMAL
    };

    public AePlayWave(String wavfile) { 
        filename = wavfile;
        curPosition = Position.NORMAL;
    } 

    public AePlayWave(String wavfile, Position p) { 
        filename = wavfile;
        curPosition = p;
    } 

    public void run() { 

        File soundFile = new File(filename);
        if (!soundFile.exists()) { 
            System.err.println("Wave file not found: " + filename);
            return;
        } 

        AudioInputStream audioInputStream = null;
        try { 
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (UnsupportedAudioFileException e1) { 
            e1.printStackTrace();
            return;
        } catch (IOException e1) { 
            e1.printStackTrace();
            return;
        } 

        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine auline = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        try { 
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
        } catch (LineUnavailableException e) { 
            e.printStackTrace();
            return;
        } catch (Exception e) { 
            e.printStackTrace();
            return;
        } 

        if (auline.isControlSupported(FloatControl.Type.PAN)) { 
            FloatControl pan = (FloatControl) auline
                    .getControl(FloatControl.Type.PAN);
            if (curPosition == Position.RIGHT) 
                pan.setValue(1.0f);
            else if (curPosition == Position.LEFT) 
                pan.setValue(-1.0f);
        } 

        auline.start();
        int nBytesRead = 0;
        byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

        try { 
            while (nBytesRead != -1) { 
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                if (nBytesRead >= 0) 
                    auline.write(abData, 0, nBytesRead);
            } 
        } catch (IOException e) { 
            e.printStackTrace();
            return;
        } finally { 
            auline.drain();
            auline.close();
        } 

    } 
}

这篇关于在我的游戏中有 .wav、.au 声音后极度滞后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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