Java声音只能播放一次?

标签 java audio playback

我使用下面的方法来播放录制的声音,效果很好,但是为什么只播放一次,第二次点击播放按钮时,什么也没做,如何重置数据?

  // Write data to the OutputChannel.
  public class Playback implements Runnable
  {
    SourceDataLine line;
    Thread thread;

    public void start()
    {
      errStr=null;
      thread=new Thread(this);
      thread.setName("Playback");
      thread.start();
    }

    public void stop() { thread=null; }

    private void shutDown(String message)
    {
      if ((errStr=message)!=null)
      {
        System.err.println(errStr);
        samplingGraph.repaint();
      }
      if (thread!=null)
      {
        thread=null;
        samplingGraph.stop();
        if (Java_Sound.Running_In_All_Permissions_Mode) captB.setEnabled(true);
        pausB.setEnabled(false);
        playB.setText("Play");
      }
    }

    public void run()
    {
      AudioFormat format=formatControls.getFormat();                                               // get an AudioInputStream of the desired format for playback
      AudioInputStream playbackInputStream=AudioSystem.getAudioInputStream(format,audioInputStream);

      if (playbackInputStream==null)
      {
        shutDown("Unable to convert stream of format "+audioInputStream+" to format "+format);
        return;
      }
      SourceDataLine.Info info=new DataLine.Info(SourceDataLine.class,format);                     // define the required attributes for our line,and make sure a compatible line is supported.

      if (AudioSystem.isLineSupported(info))
      {
         try                                                                                        // get and open the source data line for playback.
        {
          line=(SourceDataLine)AudioSystem.getLine(info);
          line.open(format,bufSize);
        }
        catch (LineUnavailableException ex)
        {
          shutDown("Unable to open the line: "+ex);
          return;
        }
        int frameSizeInBytes=format.getFrameSize();                                                // play back the captured audio data
        int bufferLengthInFrames=line.getBufferSize()/8;
        int bufferLengthInBytes=bufferLengthInFrames*frameSizeInBytes;
        byte[] data=new byte[bufferLengthInBytes];
        int numBytesRead=0;
        line.start();                                                                              // start the source data line
        while (thread!=null)
        {
          try
          {
            if ((numBytesRead=playbackInputStream.read(data))==-1) break;
            int numBytesRemaining=numBytesRead;
             while (numBytesRemaining>0) { numBytesRemaining-=line.write(data,0,numBytesRemaining); }
          }
          catch (Exception e)
          {
            shutDown("Error during playback: "+e);
            break;
          }
        }
      }
       if (thread!=null) line.drain();                                                              // we reached the end of the stream. Let the data play out,then stop and close the line.
      line.stop();
      line.close();
      line=null;
      shutDown(null);
    }
  }

经过我的测试,我发现这一行导致了问题

if ((numBytesRead=playbackInputStream.read(data))==-1)break;

第一次播放有数据,正常,第二次就坏了。为什么 ?如何解决?

最佳答案

从未使用过java音频,但由于您使用的是流,因此您需要重置流(如果该选项可用),或者需要在每次读取时创建新流。

关于Java声音只能播放一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3991581/

相关文章:

java - 有没有办法根据正在编辑的行和列来更改 ComboBoxTableCell 的内容?

audio - .wav 文件格式中的 channel 是什么?播放 wav 文件时所有 channel 是否同时播放?

android - 使用 SL4A 使用 Python 获取录制声音的振幅

audio - 无需语音识别即可检测多个声音

Android studio - play store - 上传失败 - 图标无效

loops - 如何在libgdx中循环播放音乐?

java - IPBoard 密码哈希问题,与算法不匹配

java - 通过 Microsoft Exchange 发送电子邮件

ios - swift - 背景音乐和转场

java - 在 Java 中使用 "sincos"