亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使音頻循環更快

使音頻循環更快

狐的傳說 2023-03-09 14:01:05
簡而言之,我在我的應用程序中使用 .ogg 文件,并且有幾個背景音軌需要循環播放。但是,我循環播放音頻文件的方法是簡單地重新加載音頻文件并再次播放。這種方法會在循環的每次播放之間產生延遲,這對于游戲所期望的無縫體驗來說是不理想的。有沒有一種方法我不必每次都重新加載文件?如有必要,我愿意將音頻文件保存在內存中。這是我的 Sound 類,它的功能有所減少,可以解決問題的核心:以下庫可能是播放某些文件類型所必需的,并且應該與上述文件一起編譯:(鏈接)對于未來的讀者,以防上述鏈接過期,使用的庫如下:jl1.0.1.jarjogg-0.0.7.jarjorbis-0.0.17-1.jarmp3spi1.9.5.jarvorbisspi1.0.3.jar使用這個 Sound 類和這個 ogg 文件(來自 Undertale 的 Spear of Justice),這是一個顯示問題的簡單類:import javax.sound.sampled.UnsupportedAudioFileException;import java.io.IOException;public class Test {    public static void main(String[] args) throws IOException, UnsupportedAudioFileException, InterruptedException {        //Replace the path with the path to the downloaded soj.ogg file or another test file        Sound spearOfJustice = new Sound("C:\\Users\\gigibayte\\Desktop\\soj.ogg", true);        spearOfJustice.play();        //Ensure that this is greater than or equal than the length of the audio file chosen above in seconds.        int songSeconds = 240;        //Song is played twice to show looping issue        Thread.sleep(songSeconds * 2 * 1000);    }}
查看完整描述

1 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

實際上,解決方案比我想象的要容易得多。我只是將 do-while 循環移動到流方法并相應地進行了更改。


        PlayingSound() {

            Thread playingSound = new Thread(() -> {


                //REMOVED THE DO WHILE LOOP HERE

                try {

                    AudioInputStream in;

                    in = getAudioInputStream(new File(fileName));

                    final AudioFormat outFormat = getOutFormat(in.getFormat());

                    final Info info = new Info(SourceDataLine.class, outFormat);

                    try(final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info)) {

                        if(line != null) {

                            line.open(outFormat);

                            line.start();

                            AudioInputStream inputMystream = AudioSystem.getAudioInputStream(outFormat, in);

                            stream(outFormat, inputMystream, line);

                            line.drain();

                            line.stop();

                        }

                    }

                }

                catch(UnsupportedAudioFileException | LineUnavailableException | IOException e) {

                    throw new IllegalStateException(e);

                }

                finally {

                    removeInternalSound(this);

                }

            });

            playingSound.start();

        }


        /**

         * Streams the audio to the mixer

         *

         * @param in   Input stream to audio file

         * @param line Where the audio data can be written to

         * @throws IOException Thrown if given file has any problems

         */

        private void stream(AudioFormat outFormat, AudioInputStream in, SourceDataLine line) throws IOException {

            byte[] buffer = new byte[32];

            do {

                for(int n = 0; n != -1 && !stop; n = in.read(buffer, 0, buffer.length)) {

                    byte[] bufferTemp = new byte[buffer.length];

                    for(int i = 0; i < bufferTemp.length; i += 2) {

                        short audioSample = (short) ((short) ((buffer[i + 1] & 0xff) << 8) | (buffer[i] & 0xff));

                        bufferTemp[i] = (byte) audioSample;

                        bufferTemp[i + 1] = (byte) (audioSample >> 8);

                    }

                    buffer = bufferTemp;

                    line.write(buffer, 0, n);

                }

                in = getAudioInputStream(new File(fileName));

                in = AudioSystem.getAudioInputStream(outFormat, in);

            } while(loopable && !stop);

        }



查看完整回答
反對 回復 2023-03-09
  • 1 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號