2

I do need you help. How can I adjust the following code in order to play a .wav file backwards?

Any help will be very much appreciated..Thanks. Carlos

import java.io.*;
import javax.sound.sampled.*;

public class WavPlay {
   public static void main(String[] args) {
      SourceDataLine soundLine = null;
      int BUFFER_SIZE = 64*1024;  // 64 KB

      // Set up an audio input stream piped from the sound file.
      try {
         File soundFile = new File("chord.wav");
         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile);
         AudioFormat audioFormat = audioInputStream.getFormat();
         DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
         soundLine = (SourceDataLine) AudioSystem.getLine(info);
         soundLine.open(audioFormat);
         soundLine.start();
         System.out.println("File chord.wav....playing");
         int nBytesRead = 0;
         byte[] sampledData = new byte[BUFFER_SIZE];

         while (nBytesRead != -1) {
            nBytesRead = audioInputStream.read(sampledData, 0, sampledData.length);
            if (nBytesRead >= 0) {
               // Writes audio data to the mixer via this source data line.
               soundLine.write(sampledData, 0, nBytesRead);

            }

         }
      } catch (UnsupportedAudioFileException ex) {
         ex.printStackTrace();
      } catch (IOException ex) {
         ex.printStackTrace();
      } catch (LineUnavailableException ex) {
         ex.printStackTrace();
      } finally {
         soundLine.drain();
         soundLine.close();
      }
   }

}
Radiodef
  • 37,180
  • 14
  • 90
  • 125
carlos
  • 21
  • 1
  • 2

1 Answers1

0

Yes! It is possible, a long time ago I've made a bounty for it. Here is the link to my question. And the problem was that I had to find it out my own way.

Community
  • 1
  • 1
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – blo0p3r Oct 29 '14 at 11:41
  • @blo0p3r: It's a link to my own question on Stackoverflow. That is never going to change... – Martijn Courteaux Oct 30 '14 at 08:59
  • Following `meta` : http://meta.stackoverflow.com/q/253963/686036. If not this, the question as a whole should be marked as duplicate. – blo0p3r Oct 30 '14 at 12:48
  • @MartijnCourteaux I have a similar question here if you can help :) https://stackoverflow.com/questions/51920844/java-audioinputstream-how-to-support-skip-with-negative-number-of-bytes – GOXR3PLUS Aug 19 '18 at 18:55