下面列出了javax.sound.sampled.AudioSystem#getAudioInputStream ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Convert signal to signed PCM for further processing.
*
* @param file audio file
* @return PCM audio inputstream
* @throws UnsupportedAudioFileException if the file is not supported by {@link AudioSystem}.
* @throws IOException if something goes wrong while opening the audio file
*/
private static AudioInputStream openStream(final File file) throws UnsupportedAudioFileException, IOException {
final AudioInputStream origAudioInputStream = AudioSystem.getAudioInputStream(file);
final AudioFormat origFormat = origAudioInputStream.getFormat();
return AudioSystem.getAudioInputStream(
new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
origFormat.getSampleRate(),
origFormat.getSampleSizeInBits() <= 0 ? 16 : origFormat.getSampleSizeInBits(),
origFormat.getChannels(),
origFormat.getFrameSize() <= 0 ? origFormat.getChannels() * 2 : origFormat.getFrameSize(),
origFormat.getSampleRate(),
false),
origAudioInputStream
);
}
private void playSirenSound() {
try {
File soundFile = new File(sirenFile);
AudioInputStream soundIn = AudioSystem.getAudioInputStream(soundFile);
AudioFormat format = soundIn.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
soundOn = false;
}
}
});
clip.open(soundIn);
clip.start();
soundOn = true;
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* WAVE�t�@�C�������[�h
* @param url WAVE�t�@�C����URL
*/
public static void load(URL url) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
// �I�[�f�B�I�X�g���[�����J��
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
// WAVE�t�@�C���̃t�H�[�}�b�g���擾
AudioFormat format = ais.getFormat();
// ���C�����擾
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format, AudioSystem.NOT_SPECIFIED);
// WAVE�f�[�^���擾
DataClip clip = new DataClip(ais);
// WAVE�f�[�^��o�^
clips[counter] = clip;
lines[counter] = (SourceDataLine)AudioSystem.getLine(info);
// ���C�����J��
lines[counter].open(format);
counter++;
}
/**
* WAVE�t�@�C�������[�h
* @param url WAVE�t�@�C����URL
*/
public static void load(URL url) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
// �I�[�f�B�I�X�g���[�����J��
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
// WAVE�t�@�C���̃t�H�[�}�b�g���擾
AudioFormat format = ais.getFormat();
// ���C�����擾
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format, AudioSystem.NOT_SPECIFIED);
// WAVE�f�[�^���擾
DataClip clip = new DataClip(ais);
// WAVE�f�[�^��o�^
clips[counter] = clip;
lines[counter] = (SourceDataLine)AudioSystem.getLine(info);
// ���C�����J��
lines[counter].open(format);
counter++;
}
/**
* Loads and returns a Clip from a resource.
* @param ref the resource name
* @param isMP3 true if MP3, false if WAV
* @return the loaded and opened clip
*/
private static MultiClip loadClip(String ref, boolean isMP3) {
try {
URL url = ResourceLoader.getResource(ref);
// check for 0 length files
InputStream in = url.openStream();
if (in.available() == 0) {
in.close();
return new MultiClip(ref, null);
}
in.close();
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
return loadClip(ref, audioIn, isMP3);
} catch (Exception e) {
softErr(e, "Failed to load clip %s", ref);
return null;
}
}
@Override
public void run() {
log("ConversionThread[" + num + "] started.");
try {
InputStream inStream = new ByteArrayInputStream(pcmBuffer);
AudioInputStream pcmStream = new AudioInputStream(
inStream, pcmFormat, AudioSystem.NOT_SPECIFIED);
AudioInputStream alawStream = AudioSystem.getAudioInputStream(alawFormat, pcmStream);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
int read = 0;
byte[] data = new byte[4096];
while((read = alawStream.read(data)) != -1) {
outStream.write(data, 0, read);
}
alawStream.close();
resultArray = outStream.toByteArray();
} catch (Exception ex) {
log("ConversionThread[" + num + "] exception:");
log(ex);
}
log("ConversionThread[" + num + "] completed.");
}
@Override
public Clip loadAsset( InputStream input, AssetInfo assetInfo ) throws Exception
{
AudioInputStream ais = AudioSystem.getAudioInputStream( input );
Clip clip = AudioSystem.getClip();
clip.open( ais );
return clip;
}
public static void main(String args[]) throws Exception {
System.out.println();
System.out.println();
System.out.println("4399551: Repost of bug candidate: cannot replay aif file (Review ID: 108108)");
// try to read this file
AudioSystem.getAudioInputStream(new ByteArrayInputStream(SHORT_AIFC_ULAW));
System.out.println(" test passed.");
}
/**
* Tests the {@code AudioInputStream} fetched from the fake header.
* <p>
* Note that the frameLength is stored as long which means
* that {@code AudioInputStream} must store all possible data from au file.
*/
private static void testAIS(final byte[] type, final int rate,
final int channel, final long size)
throws Exception {
final byte[] header = createHeader(type, rate, channel, size);
final ByteArrayInputStream fake = new ByteArrayInputStream(header);
final AudioInputStream ais = AudioSystem.getAudioInputStream(fake);
final AudioFormat format = ais.getFormat();
final long frameLength = size / format.getFrameSize();
if (size != MAX_UNSIGNED_INT) {
if (frameLength != ais.getFrameLength()) {
System.err.println("Expected: " + frameLength);
System.err.println("Actual: " + ais.getFrameLength());
throw new RuntimeException();
}
} else {
if (ais.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
System.err.println("Actual: " + ais.getFrameLength());
throw new RuntimeException();
}
}
if (ais.available() < 0) {
System.err.println("available should be >=0: " + ais.available());
throw new RuntimeException();
}
validateFormat(type[1], rate, channel, format);
}
public static Clip readMusicFile(InputStream audioFilePath, SoundOutput soundOutput) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFilePath);
AudioFormat format = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Mixer mixer = soundOutput.getMixer();
Clip audioClip = (Clip) mixer.getLine(info);
audioClip.open(audioStream);
return audioClip;
}
public JavaSoundAudioClip(InputStream in) throws IOException {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.<init>");
BufferedInputStream bis = new BufferedInputStream(in, STREAM_BUFFER_SIZE);
bis.mark(STREAM_BUFFER_SIZE);
boolean success = false;
try {
AudioInputStream as = AudioSystem.getAudioInputStream(bis);
// load the stream data into memory
success = loadAudioData(as);
if (success) {
success = false;
if (loadedAudioByteLength < CLIP_THRESHOLD) {
success = createClip();
}
if (!success) {
success = createSourceDataLine();
}
}
} catch (UnsupportedAudioFileException e) {
// not an audio file
try {
MidiFileFormat mff = MidiSystem.getMidiFileFormat(bis);
success = createSequencer(bis);
} catch (InvalidMidiDataException e1) {
success = false;
}
}
if (!success) {
throw new IOException("Unable to create AudioClip from input stream");
}
}
public static synchronized Clip playback(URL url, float addVolume) {
try {
try ( AudioInputStream in = AudioSystem.getAudioInputStream(url)) {
return playback(in, addVolume);
}
} catch (Exception e) {
logger.debug(e.toString());
return null;
}
}
public static AudioFloatInputStream getInputStream(InputStream stream)
throws UnsupportedAudioFileException, IOException {
return new DirectAudioFloatInputStream(AudioSystem
.getAudioInputStream(stream));
}
public static Sample open(final File file) throws Exception {
final AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
return open(audioStream);
}
/**
* Inits Audio ressources from InputStream.
*/
protected void initAudioInputStream(final InputStream inputStream)
throws UnsupportedAudioFileException, IOException {
m_audioInputStream = AudioSystem.getAudioInputStream(inputStream);
m_audioFileFormat = AudioSystem.getAudioFileFormat(inputStream);
}
public static AudioFloatInputStream getInputStream(File file)
throws UnsupportedAudioFileException, IOException {
return new DirectAudioFloatInputStream(AudioSystem
.getAudioInputStream(file));
}
public static AudioFloatInputStream getInputStream(URL url)
throws UnsupportedAudioFileException, IOException {
return new DirectAudioFloatInputStream(AudioSystem
.getAudioInputStream(url));
}
public static AudioFloatInputStream getInputStream(InputStream stream)
throws UnsupportedAudioFileException, IOException {
return new DirectAudioFloatInputStream(AudioSystem
.getAudioInputStream(stream));
}
/**
*
* @param audioFile
* audiofile
* @param line
* line
* @param lineListener
* lineListener
* @param outputMode
* if MONO, force output to be mono; if STEREO, force output to
* be STEREO; if LEFT_ONLY, play a mono signal over the left
* channel of a stereo output, or mute the right channel of a
* stereo signal; if RIGHT_ONLY, do the same with the right
* output channel.
* @throws IOException
* IOException
* @throws UnsupportedAudioFileException
* UnsupportedAudioFileException
*/
public AudioPlayer(File audioFile, SourceDataLine line, LineListener lineListener, int outputMode)
throws IOException, UnsupportedAudioFileException {
this.ais = AudioSystem.getAudioInputStream(audioFile);
this.line = line;
this.lineListener = lineListener;
this.outputMode = outputMode;
}
/**
*
* @param audioFile
* audiofile
* @param line
* line
* @param lineListener
* lineListener
* @param outputMode
* if MONO, force output to be mono; if STEREO, force output to
* be STEREO; if LEFT_ONLY, play a mono signal over the left
* channel of a stereo output, or mute the right channel of a
* stereo signal; if RIGHT_ONLY, do the same with the right
* output channel.
* @throws IOException
* IOException
* @throws UnsupportedAudioFileException
* UnsupportedAudioFileException
*/
public AudioPlayer(File audioFile, SourceDataLine line, LineListener lineListener, int outputMode)
throws IOException, UnsupportedAudioFileException {
this.ais = AudioSystem.getAudioInputStream(audioFile);
this.line = line;
this.lineListener = lineListener;
this.outputMode = outputMode;
}