-1

So, I am to write a copy of the sound that I manipulated into a playable format, but whenever I try it, the files are very small (< 1kb), and they don't play, most likely because it is not readable data. Here is the code

 public void play(int pause, String filename, String path) throws InterruptedException {   
    for (int i =0; i < numWords; i++) {
        myWordArray[i].blockingPlay();
        Thread.sleep(pause); // 300 m.seconds, as listed in main method parameter 
        File file = new File(path, filename);
    try{
    PrintWriter output = new PrintWriter(file);
    output.println(myWordArray[i]);
    output.close(); 
    } catch(IOException ex) {
        System.out.printf("ERROR: %s\n", ex);
    }
    }
}

I'm guessing PrintWriter only works for strings, which is why my WAV file is getting corrupted when I try to play? What should I substitute for this?

EDIT-

So, using the guides posted in the comments, I modified the code, however, now it does not create a file at all. Here is the code in my audio class...

 public void play(int pause, String filename, String path) throws InterruptedException {   
    for (int i =0; i < numWords; i++) {
        myWordArray[i].blockingPlay();
        Thread.sleep(pause); // 300 m.seconds, as listed in main method parameter 
        File file = new File("C:\\Users\\Justin\\Desktop\\JavaMedia\\", "thisisatest.wav");
        try{
        InputStream fis = new FileInputStream(file);
        byte[] buffer = new byte[(int)file.length()];
        fis.read(buffer, 0, buffer.length);
        fis.close();
        }
        catch(IOException ex){
              System.out.printf("ERROR: %s\n", ex);
}
}
}

Here is the call in my main...

myPoem.play(300,"thisisatest_pause.wav", "C:\\Users\\Justin\\Desktop\\JavaMedia\\");

I want the "thisisatest_pause.wav" to be the new file it creates, but it doesn't create one at all.

  • maybe a ByteArrayOutputStream or similar – Scary Wombat Oct 04 '16 at 05:38
  • Possible duplicate of [Convert audio stream to WAV byte array in Java without temp file](http://stackoverflow.com/questions/198679/convert-audio-stream-to-wav-byte-array-in-java-without-temp-file) – BevynQ Oct 04 '16 at 05:40
  • I edited it and stated my error, very confused as to how to do this, even with using the guides. – TheDkmariolink Oct 04 '16 at 16:39

1 Answers1

0

Because myWordArray[i] is an Object. So it will write myWordArray[i].toString() to the file, not the sound.
Are you using Sound#blockingPlay() from http://coweb.cc.gatech.edu/mediaComp-plan/uploads/101/bookClasses-3-9-10.zip. If yes, it has writeToFile() method to write a sound to file.

Lucas Pham
  • 26
  • 4