简体   繁体   English

Java的:序列化和反序列化的音频文件

[英]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. 现在,我正在用动画,组合等创建Character maker。我已经对我的sprite遇到了一些问题,因为BufferedImage无法序列化。 This was solved with PixelGrabber - when i click saveButton, this pixelGrabber grabs pixels from Image and saves them as array. 这是通过PixelGrabber解决的-当我单击saveButton时,此pixelGrabber从Image抓取像素并将其保存为数组。 This array can be serialized then. 然后可以将该数组序列化。 It can be deserialized, when i load this project, and used as an Image again. 当我加载此项目时,可以将其反序列化,并再次用作Image。 Now, my question - is it possible to save .wav file as an serializable array? 现在,我的问题-是否可以将.wav文件另存为可序列化的数组? And after this to deserialize and use it again as an audio file? 在此之后反序列化并将其再次用作音频文件吗? pssorry for my english pssorry我的英语

Here is a simple framework for working with WAV files: http://www.labbookpages.co.uk/audio/javaWavFiles.html 这是使用WAV文件的简单框架: 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: 我很快就写下来,因此如果有任何错误我深表歉意,但是将wav文件加载到ArrayList中的样子如下:

//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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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