简体   繁体   中英

Java: Serialize and Deserialize audio files

Im writing a small fighting-game. Right now im creating Character maker with animations, combos, etc. I allready had some problems with my sprites, because BufferedImage can not be serialized. This was solved with PixelGrabber - when i click saveButton, this pixelGrabber grabs pixels from Image and saves them as array. This array can be serialized then. It can be deserialized, when i load this project, and used as an Image again. Now, my question - is it possible to save .wav file as an serializable array? And after this to deserialize and use it again as an audio file? pssorry for my english

Here is a simple framework for working with WAV files: http://www.labbookpages.co.uk/audio/javaWavFiles.html

I wrote this out pretty quickly so I apologize if there are any mistakes, but here is what loading the wav file into an ArrayList would look like:

//make sure to import java.util.ArrayList; 
try {
    // load the file, set up the buffer
    WavFile gameWav = WavFile.openWavFile( new File( "game_sound.wav" ) );
    ArrayList<double> gameWavArray = new ArrayList<double>();
    long framesRead = 0;
    long totalFrames = gameWav.getNumFrames();
    //read the buffer in 1000 frames at a time
    do {
        double[] gameWavBuffer = new double[1000];
        // Read the frames into array, increment framesRead
        framesRead = framesRead + Long.valueOf( gameWav.readFrames(gameWavBuffer, framesRead, 1000) );
        //add all of the new frames to our ArrayList
        Collections.addAll(gameWavArray, gameWavBuffer );
    }
    while (framesRead < totalFrames );
}
catch (Exception e) {
     System.err.println(e);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM