本文整理匯總了Java中javax.sound.sampled.AudioInputStream類的典型用法代碼示例。如果您正苦於以下問題:Java AudioInputStream類的具體用法?Java AudioInputStream怎麽用?Java AudioInputStream使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
AudioInputStream類屬於javax.sound.sampled包,在下文中一共展示了AudioInputStream類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: Sound
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
/**
* Creates a new Sound instance by the specified file path. Loads the sound
* data into a byte array and also retrieves information about the format of
* the sound file.
*
* Note that the constructor is private. In order to load files use the static
* methods {@link #find(String)} or {@link #load(String)} methods depending on
* whether you already loaded the sound or not.
*
* @param is
* The input stream to load the sound from.
*/
private Sound(InputStream is, String name) {
this.name = name;
try {
AudioInputStream in = AudioSystem.getAudioInputStream(is);
if (in != null) {
final AudioFormat baseFormat = in.getFormat();
final AudioFormat decodedFormat = this.getOutFormat(baseFormat);
// Get AudioInputStream that will be decoded by underlying VorbisSPI
in = AudioSystem.getAudioInputStream(decodedFormat, in);
this.stream = in;
this.streamData = StreamUtilities.getBytes(this.stream);
this.format = this.stream.getFormat();
}
} catch (final UnsupportedAudioFileException | IOException e) {
log.log(Level.SEVERE, e.getMessage(), e);
}
}
示例2: getAudioFileTypes
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
@Override
public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
System.arraycopy(types, 0, filetypes, 0, types.length);
// make sure we can write this stream
AudioFormat format = stream.getFormat();
AudioFormat.Encoding encoding = format.getEncoding();
if( AudioFormat.Encoding.ALAW.equals(encoding) ||
AudioFormat.Encoding.ULAW.equals(encoding) ||
AudioFormat.Encoding.PCM_SIGNED.equals(encoding) ||
AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) ) {
return filetypes;
}
return new AudioFileFormat.Type[0];
}
示例3: getAudioInputStream
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
@Override
public AudioInputStream getAudioInputStream(final InputStream stream)
throws UnsupportedAudioFileException, IOException {
final StandardFileFormat format = getAudioFileFormat(stream);
final AudioFormat af = format.getFormat();
final long length = format.getLongFrameLength();
// we've got everything, the stream is supported and it is at the
// beginning of the header, so find the data chunk again and return an
// AudioInputStream
final RIFFReader riffiterator = new RIFFReader(stream);
while (riffiterator.hasNextChunk()) {
RIFFReader chunk = riffiterator.nextChunk();
if (chunk.getFormat().equals("data")) {
return new AudioInputStream(chunk, af, length);
}
}
throw new UnsupportedAudioFileException();
}
示例4: getAudioInputStream
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
/**
* This method is a replacement for
* AudioSystem.getAudioInputStream(AudioFormat, AudioInputStream), which is
* used for audio format conversion at the stream level. This method includes
* a workaround for converting from an mp3 AudioInputStream when the sketch
* is running in an applet. The workaround was developed by the Tritonus team
* and originally comes from the package javazoom.jlgui.basicplayer
*
* @param targetFormat
* the AudioFormat to convert the stream to
* @param sourceStream
* the stream containing the unconverted audio
* @return an AudioInputStream in the target format
*/
AudioInputStream getAudioInputStream(AudioFormat targetFormat,
AudioInputStream sourceStream)
{
try
{
return AudioSystem.getAudioInputStream(targetFormat, sourceStream);
}
catch (IllegalArgumentException iae)
{
debug("Using AppletMpegSPIWorkaround to get codec");
try
{
Class.forName("javazoom.spi.mpeg.sampled.convert.MpegFormatConversionProvider");
return new javazoom.spi.mpeg.sampled.convert.MpegFormatConversionProvider().getAudioInputStream(
targetFormat,
sourceStream);
}
catch (ClassNotFoundException cnfe)
{
throw new IllegalArgumentException("Mpeg codec not properly installed");
}
}
}
示例5: loadSound
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
/**
* WAV files only
*
* @param name
* Name to store sound as
* @param file
* Sound file
*/
public static void loadSound(String name, String file) {
try {
System.out.print("Loading sound file: \"" + file + "\" into clip: \"" + name + "\", ");
BufferedInputStream in = new BufferedInputStream(SoundPlayer.class.getResourceAsStream(file));
AudioInputStream ain = AudioSystem.getAudioInputStream(in);
Clip c = AudioSystem.getClip();
c.open(ain);
c.setLoopPoints(0, -1);
clips.put(name, c);
ain.close();
in.close();
System.out.println("Done.");
} catch (Exception e) {
System.out.println("Failed. (" + e.getMessage() + ")");
}
}
示例6: testAfterSaveToFile
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
/**
* Verifies the frame length after the stream was saved/read to/from file.
*/
private static void testAfterSaveToFile(final AudioFileWriter afw,
final AudioFileFormat.Type type,
AudioInputStream ais)
throws IOException {
final File temp = File.createTempFile("sound", ".tmp");
try {
afw.write(ais, type, temp);
ais = AudioSystem.getAudioInputStream(temp);
final long frameLength = ais.getFrameLength();
ais.close();
validate(frameLength);
} catch (IllegalArgumentException | UnsupportedAudioFileException
ignored) {
} finally {
Files.delete(Paths.get(temp.getAbsolutePath()));
}
}
示例7: readStream
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
private void readStream(AudioInputStream as, long byteLen) throws IOException {
// arrays "only" max. 2GB
int intLen;
if (byteLen > 2147483647) {
intLen = 2147483647;
} else {
intLen = (int) byteLen;
}
loadedAudio = new byte[intLen];
loadedAudioByteLength = 0;
// this loop may throw an IOException
while (true) {
int bytesRead = as.read(loadedAudio, loadedAudioByteLength, intLen - loadedAudioByteLength);
if (bytesRead <= 0) {
as.close();
break;
}
loadedAudioByteLength += bytesRead;
}
}
示例8: write
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
public void write(AudioInputStream stream, RIFFWriter writer)
throws IOException {
RIFFWriter fmt_chunk = writer.writeChunk("fmt ");
AudioFormat format = stream.getFormat();
fmt_chunk.writeUnsignedShort(3); // WAVE_FORMAT_IEEE_FLOAT
fmt_chunk.writeUnsignedShort(format.getChannels());
fmt_chunk.writeUnsignedInt((int) format.getSampleRate());
fmt_chunk.writeUnsignedInt(((int) format.getFrameRate())
* format.getFrameSize());
fmt_chunk.writeUnsignedShort(format.getFrameSize());
fmt_chunk.writeUnsignedShort(format.getSampleSizeInBits());
fmt_chunk.close();
RIFFWriter data_chunk = writer.writeChunk("data");
byte[] buff = new byte[1024];
int len;
while ((len = stream.read(buff, 0, buff.length)) != -1)
data_chunk.write(buff, 0, len);
data_chunk.close();
}
示例9: main
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
public static void main(String[] params) throws Exception {
AudioInputStream is =
AudioSystem.getAudioInputStream(new
ByteArrayInputStream(new byte[] {
(byte)0x2E, (byte)0x73, (byte)0x6E, (byte)0x64, (byte)0x00,
(byte)0x00, (byte)0x00, (byte)0x18,
(byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00,
(byte)0x00, (byte)0x00, (byte)0x03,
(byte)0x00, (byte)0x00, (byte)0x1F, (byte)0x40, (byte)0x00,
(byte)0x00, (byte)0x00, (byte)0x01,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
(byte)0x00, (byte)0x00, (byte)0x00,
}));
if (is.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
System.out.println("frame length should be NOT_SPECIFIED, but is: "+is.getFrameLength());
failed=true;
}
//assertTrue(is.getFrameLength() == AudioSystem.NOT_SPECIFIED);
//assertTrue(is.read(new byte[8]) == 8);
//assertTrue(is.read(new byte[2]) == -1);
if (failed) throw new Exception("Test FAILED!");
System.out.println("Test Passed.");
}
示例10: getAudioInputStream
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
/**
* Obtains an audio stream from the File provided. The File must
* point to valid audio file data.
* @param file the File for which the <code>AudioInputStream</code> should be
* constructed
* @return an <code>AudioInputStream</code> object based on the audio file data pointed
* to by the File
* @throws UnsupportedAudioFileException if the File does not point to valid audio
* file data recognized by the system
* @throws IOException if an I/O exception occurs
*/
public AudioInputStream getAudioInputStream(File file)
throws UnsupportedAudioFileException, IOException {
FileInputStream fis = new FileInputStream(file); // throws IOException
AudioFileFormat fileFormat = null;
// part of fix for 4325421
try {
fileFormat = getCOMM(fis, false);
} finally {
if (fileFormat == null) {
fis.close();
}
}
return new AudioInputStream(fis, fileFormat.getFormat(), fileFormat.getFrameLength());
}
示例11: readStream
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
private void readStream(AudioInputStream as) throws IOException {
DirectBAOS baos = new DirectBAOS();
byte buffer[] = new byte[16384];
int bytesRead = 0;
int totalBytesRead = 0;
// this loop may throw an IOException
while( true ) {
bytesRead = as.read(buffer, 0, buffer.length);
if (bytesRead <= 0) {
as.close();
break;
}
totalBytesRead += bytesRead;
baos.write(buffer, 0, bytesRead);
}
loadedAudio = baos.getInternalBuffer();
loadedAudioByteLength = totalBytesRead;
}
示例12: getAudioFileTypes
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
System.arraycopy(types, 0, filetypes, 0, types.length);
// make sure we can write this stream
AudioFormat format = stream.getFormat();
AudioFormat.Encoding encoding = format.getEncoding();
if( (AudioFormat.Encoding.ALAW.equals(encoding)) ||
(AudioFormat.Encoding.ULAW.equals(encoding)) ||
(AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) ||
(AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ) {
return filetypes;
}
return new AudioFileFormat.Type[0];
}
示例13: getPCMConvertedAudioInputStream
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
public static AudioInputStream getPCMConvertedAudioInputStream(AudioInputStream ais) {
// we can't open the device for non-PCM playback, so we have
// convert any other encodings to PCM here (at least we try!)
AudioFormat af = ais.getFormat();
if( (!af.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) &&
(!af.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED))) {
try {
AudioFormat newFormat =
new AudioFormat( AudioFormat.Encoding.PCM_SIGNED,
af.getSampleRate(),
16,
af.getChannels(),
af.getChannels() * 2,
af.getSampleRate(),
Platform.isBigEndian());
ais = AudioSystem.getAudioInputStream(newFormat, ais);
} catch (Exception e) {
if (Printer.err) e.printStackTrace();
ais = null;
}
}
return ais;
}
示例14: getAudioInputStream
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
public AudioInputStream getAudioInputStream(InputStream stream)
throws UnsupportedAudioFileException, IOException {
AudioFileFormat format = getAudioFileFormat(stream);
RIFFReader riffiterator = new RIFFReader(stream);
if (!riffiterator.getFormat().equals("RIFF"))
throw new UnsupportedAudioFileException();
if (!riffiterator.getType().equals("WAVE"))
throw new UnsupportedAudioFileException();
while (riffiterator.hasNextChunk()) {
RIFFReader chunk = riffiterator.nextChunk();
if (chunk.getFormat().equals("data")) {
return new AudioInputStream(chunk, format.getFormat(), chunk
.getSize());
}
}
throw new UnsupportedAudioFileException();
}
示例15: getAudioInputStream
import javax.sound.sampled.AudioInputStream; //導入依賴的package包/類
public AudioInputStream getAudioInputStream(AudioFormat targetFormat,
AudioFloatInputStream sourceStream) {
if (!isConversionSupported(targetFormat, sourceStream.getFormat()))
throw new IllegalArgumentException("Unsupported conversion: "
+ sourceStream.getFormat().toString() + " to "
+ targetFormat.toString());
if (targetFormat.getChannels() != sourceStream.getFormat()
.getChannels())
sourceStream = new AudioFloatInputStreamChannelMixer(sourceStream,
targetFormat.getChannels());
if (Math.abs(targetFormat.getSampleRate()
- sourceStream.getFormat().getSampleRate()) > 0.000001)
sourceStream = new AudioFloatInputStreamResampler(sourceStream,
targetFormat);
return new AudioInputStream(new AudioFloatFormatConverterInputStream(
targetFormat, sourceStream), targetFormat, sourceStream
.getFrameLength());
}