Java examples for javax.sound.sampled:Audio
Reads the complete contents of a small audio file into a byte array
//package com.java2s; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.UnsupportedAudioFileException; public class Main { /**//from w w w .j a v a 2 s . c om * Reads the complete contents of a small audio file into a byte array * * @param fileName * the audio file * @return byte array with the contents read from the file * * @throws UnsupportedAudioFileException * if there is a problem with audio format of the file not being * supported by Java * @throws IOException * if there is a problem reading from the file * @throws FileNotFoundException * if the file does not exist or cannot be opened */ public static byte[] audioFileToByteArray(String fileName) throws UnsupportedAudioFileException, IOException, FileNotFoundException { if (!new File(fileName).isFile()) { throw new FileNotFoundException("Error the audio file: " + fileName + " does not exist"); } File audioFile = new File(fileName); AudioInputStream audioStream = AudioSystem .getAudioInputStream(audioFile); int numbBytes = audioStream.available(); byte[] bytesBuffer = new byte[numbBytes]; audioStream.read(bytesBuffer); audioStream.close(); return bytesBuffer; } }