简体   繁体   中英

AudioInputStream to Buffer

I am working on a web application wherein I am recording sound from my microphone using a flash plugin . After the recording I can upload the recorded file to the server. Plz see the code below:

            // Get the input stream
            InputStream is = request.getInputStream();
            InputStream bufferedIn = new BufferedInputStream(is);
            AudioInputStream ais = AudioSystem.getAudioInputStream(bufferedIn);

            // Declare the new format to convert to
            AudioFormat audioFormat =  new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);

            // Convert the format and return the new audio input stream
            ais = AudioSystem.getAudioInputStream(audioFormat, ais);

Now, after this conversion I want to save the audio data from ais into a buffer and upload it to DB.

How do I do that? Thanks!! :)

AudioInputStream extends InputStream , so you can use it directly to store to the database. When using JDBC, you could use:

ais = AudioSystem.getAudioInputStream(audioFormat, ais);
PreparedStatement prep = conn.prepareStatement("insert into data values(?, ?)");
prep.setInt(1, 1);
prep.setBinaryStream(2, ais);

Please note setBinaryStream without length parameter is only available in Java 6 and newer. Some databases may not support it.

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