第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使音頻循環(huán)更快

使音頻循環(huán)更快

狐的傳說(shuō) 2023-03-09 14:01:05
簡(jiǎn)而言之,我在我的應(yīng)用程序中使用 .ogg 文件,并且有幾個(gè)背景音軌需要循環(huán)播放。但是,我循環(huán)播放音頻文件的方法是簡(jiǎn)單地重新加載音頻文件并再次播放。這種方法會(huì)在循環(huán)的每次播放之間產(chǎn)生延遲,這對(duì)于游戲所期望的無(wú)縫體驗(yàn)來(lái)說(shuō)是不理想的。有沒(méi)有一種方法我不必每次都重新加載文件?如有必要,我愿意將音頻文件保存在內(nèi)存中。這是我的 Sound 類,它的功能有所減少,可以解決問(wèn)題的核心:以下庫(kù)可能是播放某些文件類型所必需的,并且應(yīng)該與上述文件一起編譯:(鏈接)對(duì)于未來(lái)的讀者,以防上述鏈接過(guò)期,使用的庫(kù)如下:jl1.0.1.jarjogg-0.0.7.jarjorbis-0.0.17-1.jarmp3spi1.9.5.jarvorbisspi1.0.3.jar使用這個(gè) Sound 類和這個(gè) ogg 文件(來(lái)自 Undertale 的 Spear of Justice),這是一個(gè)顯示問(wèn)題的簡(jiǎn)單類: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貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊

實(shí)際上,解決方案比我想象的要容易得多。我只是將 do-while 循環(huán)移動(dòng)到流方法并相應(yīng)地進(jìn)行了更改。


        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);

        }



查看完整回答
反對(duì) 回復(fù) 2023-03-09
  • 1 回答
  • 0 關(guān)注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)