无法同时播放声音

时间:2015-09-18 03:47:19

标签: java audio volume

我试图在点击一个jbutton的同时播放六个音轨,但点击它会播放第一首曲目并等到它完成播放第二首曲目,依此类推。这是我的代码

 button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == button) {
                System.out.println("Button Pressed");
                AudioPlayerExample2 player1 = new AudioPlayerExample2();
                AudioPlayerExample2 player2 = new AudioPlayerExample2();
                AudioPlayerExample2 player3 = new AudioPlayerExample2();
                AudioPlayerExample2 player4 = new AudioPlayerExample2();
                AudioPlayerExample2 player5 = new AudioPlayerExample2();
                AudioPlayerExample2 player6 = new AudioPlayerExample2();
                player1.play(track1);
                player2.play(track2);
                player3.play(track3);
                player4.play(track4);
                player5.play(track5);
                player6.play(track6);
            }
        }
    });

并导入音频播放器

public class AudioPlayerExample2 {

private static final int BUFFER_SIZE = 4096;


public void play(String audioFilePath) {
    File audioFile = new File(audioFilePath);
    try {
        AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);

        AudioFormat format = audioStream.getFormat();

        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        SourceDataLine audioLine = (SourceDataLine) AudioSystem.getLine(info);

        audioLine.open(format);

        audioLine.start();

        System.out.println("Playback started.");

        byte[] bytesBuffer = new byte[BUFFER_SIZE];
        int bytesRead = -1;

        while ((bytesRead = audioStream.read(bytesBuffer)) != -1) {
            audioLine.write(bytesBuffer, 0, bytesRead);
        }

        audioLine.drain();
        audioLine.close();
        audioStream.close();

        System.out.println("Playback completed.");

    } catch (UnsupportedAudioFileException ex) {
        System.out.println("The specified audio file is not supported.");
        ex.printStackTrace();
    } catch (LineUnavailableException ex) {
        System.out.println("Audio line for playing back is unavailable.");
        ex.printStackTrace();
    } catch (IOException ex) {
        System.out.println("Error playing the audio file.");
        ex.printStackTrace();
    }
}

public static void main(String[] args) {
    String audioFilePath = "";
    AudioPlayerExample2 player = new AudioPlayerExample2();
    player.play(audioFilePath);
}}

在播放曲目时,按钮也会一直被点击,因此我也无法使用我的音量jslider。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

你编写play方法的方式,它将阻塞直到流完全播放 - 这意味着流将一个接一个地播放。一种选择是为每个流分叉一个新线程。这将避免阻塞问题,但会引入另一个问题,那就是线程将全部启动。这意味着流并非都必须在完全相同的时间开始(尽管您可以使用信号使它们非常接近同步)。

我认为更好的方法是使用所有文件中的读取并在单个线程中写入 one SourceDataLine。这意味着您必须自己手动混合信号。假设您的所有文件具有相同的采样率和位深度,那么这并不困难。我假设有16位样本。如果您的文件不同,那么您可以弄清楚如何处理它。

public void play(String[] audioFilePath) {
    int numStreams = audioFilePath.length;

    // Open all of the file streams
    AudioInputStream[] audioStream = new AudioInputStream[numStreams];
    for (int i = 0; i < numStreams; i++)
    audioStream[i] = AudioSystem.getAudioInputStream(audioFile);

    // Open the audio line.
    AudioFormat format = audioStream[0].getFormat();
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
    SourceDataLine audioLine = (SourceDataLine) AudioSystem.getLine(info);
    audioLine.open(format);
    audioLine.start();

    while (true) {
        // Read a buffer from each stream and mix into an array of
        // doubles.
        byte[] bytesBuffer = new byte[BUFFER_SIZE];
        double[] mixBuffer = new double[BUFFER_SIZE/2];
        int maxSamplesRead = -1;
        for (int i = 0 ; i < numStreams; i++)
        {
            int bytesRead = audioStream.read(bytesBuffer);
            if (bytesRead != -1) {
                int samplesRead = bytesRead/2;
                if (samplesRead > maxSamplesRead) {
                    maxSamplesRead = samplesRead;
                }
                for (int j = 0 ; j < bytesRead/2 ; j++) {
                    double sample = ((bytesBuffer[j*2] << 8) | bytesBuffer[j*2+1]) / 32768.0;
                    mixBuffer[j] += sample;
                }
            }
        }

        // Convert the mixed samples back into a byte array and play.
        if (maxSamplesRead > 0) {
            for (int i = 0; i < maxSamplesRead; i++) {
                // rescale data between -1 and 1
                mixBuffer[i] /= numStreams;

                // and now back to 16-bit
                short sample16 = (short)(mixBuffer * 32768);

                // and back to bytes
                bytesBuffer[i*2]   = (byte)(sample16 >> 8);
                bytesBuffer[i*2+1] = (byte)(sample16);
            }
            audioLine.write(bytesBuffer, 0, maxSamplesRead*2);
        }
        else {
            // All of the streams are empty so cleanup.
            audioLine.drain();
            audioLine.close();
            for (int i = 0 ; i < numStreams; i++)
                audioStream[i].close();
            break;
        }
    }
}

通过传递一个文件名数组来调用它(我建议将其替换为track1,track2等...)

button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == button) {
                System.out.println("Button Pressed");
                AudioPlayerExample2 player = new AudioPlayerExample2(allTracks);
            }
        }
    });

可能更好的第三种选择是从InputStream派生一个支持多个文件的类,并在内部进行混合。使用这种方法,您可以使用大多数现有的AudioPlayerExample2类,但只有一个实例。它会比我现在想要的更多参与。

P.S。我没有尝试编译任何这些。我只是试图理解这个想法。