javax.sound.sampled.AudioSystem#getAudioFileFormat ( )源码实例Demo

下面列出了javax.sound.sampled.AudioSystem#getAudioFileFormat ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: petscii-bbs   文件: BlorbSounds.java
/**
 * {@inheritDoc}
 */
protected boolean putToDatabase(final Chunk chunk, final int resnum) {

  final InputStream aiffStream =
    new  MemoryAccessInputStream(chunk.getMemoryAccess(), 0,
        chunk.getSize() + Chunk.CHUNK_HEADER_LENGTH);
  try {

    final AudioFileFormat aiffFormat =
      AudioSystem.getAudioFileFormat(aiffStream);
    final AudioInputStream stream = new AudioInputStream(aiffStream,
      aiffFormat.getFormat(), (long) chunk.getSize());
    final Clip clip = AudioSystem.getClip();
    clip.open(stream);      
    sounds.put(resnum, new DefaultSoundEffect(clip));
    return true;

  } catch (Exception ex) {

    ex.printStackTrace();
  }
  return false;
}
 
源代码2 项目: opsu   文件: MusicController.java
/**
 * Returns the duration of the current track, in milliseconds.
 * Currently only works for MP3s.
 * @return the duration, or -1 if no track exists, else the {@code endTime}
 *         field of the beatmap loaded
 * @author Tom Brito (http://stackoverflow.com/a/3056161)
 */
public static int getDuration() {
	if (!trackExists() || lastBeatmap == null)
		return -1;

	if (duration == 0) {
		// TAudioFileFormat method only works for MP3s
		if (lastBeatmap.audioFilename.getName().endsWith(".mp3")) {
			try {
				AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(lastBeatmap.audioFilename);
				if (fileFormat instanceof TAudioFileFormat) {
					Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
					Long microseconds = (Long) properties.get("duration");
					duration = (int) (microseconds / 1000);
					return duration;
				}
			} catch (UnsupportedAudioFileException | IOException e) {}
		}

		// fallback: use beatmap end time (often not the track duration)
		duration = lastBeatmap.endTime;
	}
	return duration;
}
 
源代码3 项目: openjdk-jdk9   文件: OpenWaveFile.java
static void check(Object source) throws Exception {
     AudioFileFormat aff2 = null;
     if (source instanceof File) {
        aff2 = AudioSystem.getAudioFileFormat((File) source);
     }
     else if (source instanceof InputStream) {
        aff2 = AudioSystem.getAudioFileFormat((InputStream) source);
     }
     else if (source instanceof URL) {
        aff2 = AudioSystem.getAudioFileFormat((URL) source);
     } else throw new Exception("wrong source. Test FAILED");
     System.out.println("Got: "+aff2);
     if (aff2.getFormat().getSampleSizeInBits()==-1) {
        throw new Exception("wrong audio format. Test FAILED");
     }
}
 
源代码4 项目: opsu-dance   文件: MusicController.java
/**
 * Returns the duration of the current track, in milliseconds.
 * Currently only works for MP3s.
 * @return the duration, or -1 if no track exists, else the {@code endTime}
 *         field of the beatmap loaded
 * @author Tom Brito (http://stackoverflow.com/a/3056161)
 */
public static int getDuration() {
	if (!trackExists() || lastBeatmap == null)
		return -1;

	if (duration == 0) {
		// TAudioFileFormat method only works for MP3s
		if (lastBeatmap.audioFilename.getName().endsWith(".mp3")) {
			try {
				AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(lastBeatmap.audioFilename);
				if (fileFormat instanceof TAudioFileFormat) {
					Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
					Long microseconds = (Long) properties.get("duration");
					duration = (int) (microseconds / 1000);
					return duration;
				}
			} catch (UnsupportedAudioFileException | IOException e) {}
		}

		// fallback: use beatmap end time (often not the track duration)
		duration = lastBeatmap.endTime;
	}
	return duration;
}
 
源代码5 项目: GpsPrune   文件: AudioClip.java
/**
 * @return length of this audio clip in seconds
 */
public int getLengthInSeconds()
{
	if (_lengthInSeconds == LENGTH_UNKNOWN)
	{
		try {
			AudioFileFormat format = null;
			if (getFile() != null)
				format = AudioSystem.getAudioFileFormat(getFile());
			else
				format = AudioSystem.getAudioFileFormat(new ByteArrayInputStream(_data));
			_lengthInSeconds = (int) (format.getFrameLength() / format.getFormat().getFrameRate());
		}
		catch (Exception e) {
			_lengthInSeconds = LENGTH_NOT_AVAILABLE;
		}
	}
	return _lengthInSeconds;
}
 
源代码6 项目: dragonwell8_jdk   文件: ReadersExceptions.java
private static void test(final byte[] buffer) throws IOException {
    final InputStream is = new ByteArrayInputStream(buffer);
    try {
        AudioSystem.getAudioFileFormat(is);
    } catch (UnsupportedAudioFileException ignored) {
        // Expected.
        return;
    }
    throw new RuntimeException("Test Failed");
}
 
源代码7 项目: TencentKona-8   文件: ReadersExceptions.java
private static void test(final byte[] buffer) throws IOException {
    final InputStream is = new ByteArrayInputStream(buffer);
    try {
        AudioSystem.getAudioFileFormat(is);
    } catch (UnsupportedAudioFileException ignored) {
        // Expected.
        return;
    }
    throw new RuntimeException("Test Failed");
}
 
源代码8 项目: jdk8u60   文件: ReadersExceptions.java
private static void test(final byte[] buffer) throws IOException {
    final InputStream is = new ByteArrayInputStream(buffer);
    try {
        AudioSystem.getAudioFileFormat(is);
    } catch (UnsupportedAudioFileException ignored) {
        // Expected.
        return;
    }
    throw new RuntimeException("Test Failed");
}
 
源代码9 项目: openjdk-jdk9   文件: RecognizeHugeWaveFiles.java
/**
 * Tests the {@code AudioFileFormat} fetched from the fake header.
 * <p>
 * Note that the frameLength and byteLength are stored as int which means
 * that {@code AudioFileFormat} will store the data above {@code MAX_INT} as
 * NOT_SPECIFIED.
 */
private static void testAFF(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 AudioFileFormat aff = AudioSystem.getAudioFileFormat(fake);
    final AudioFormat format = aff.getFormat();

    if (aff.getType() != AudioFileFormat.Type.WAVE) {
        throw new RuntimeException("Error");
    }

    final long frameLength = size / format.getFrameSize();
    if (frameLength <= Integer.MAX_VALUE) {
        if (aff.getFrameLength() != frameLength) {
            System.err.println("Expected: " + frameLength);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    } else {
        if (aff.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
            System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    }
    validateFormat(type[1], rate, channel, aff.getFormat());
}
 
源代码10 项目: openjdk-jdk9   文件: RecognizeHugeWaveExtFiles.java
/**
 * Tests the {@code AudioFileFormat} fetched from the fake header.
 * <p>
 * Note that the frameLength and byteLength are stored as int which means
 * that {@code AudioFileFormat} will store the data above {@code MAX_INT} as
 * NOT_SPECIFIED.
 */
private static void testAFF(final int[] 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 AudioFileFormat aff = AudioSystem.getAudioFileFormat(fake);
    final AudioFormat format = aff.getFormat();

    if (aff.getType() != AudioFileFormat.Type.WAVE) {
        throw new RuntimeException("Error");
    }

    final long frameLength = size / format.getFrameSize();
    if (frameLength <= Integer.MAX_VALUE) {
        if (aff.getFrameLength() != frameLength) {
            System.err.println("Expected: " + frameLength);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    } else {
        if (aff.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
            System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    }
    validateFormat(type[1], rate, channel, aff.getFormat());
}
 
源代码11 项目: jdk8u-jdk   文件: ReadersExceptions.java
private static void test(final byte[] buffer) throws IOException {
    final InputStream is = new ByteArrayInputStream(buffer);
    try {
        AudioSystem.getAudioFileFormat(is);
    } catch (UnsupportedAudioFileException ignored) {
        // Expected.
        return;
    }
    throw new RuntimeException("Test Failed");
}
 
源代码12 项目: openjdk-jdk9   文件: RecognizeWaveExtensible.java
public static void main(final String[] args) throws Exception {
    final InputStream is = new ByteArrayInputStream(data);
    final AudioFileFormat aff = AudioSystem.getAudioFileFormat(is);
    System.out.println("AudioFileFormat: " + aff);
    try (AudioInputStream ais = AudioSystem.getAudioInputStream(is)) {
        System.out.println("AudioFormat: " + ais.getFormat());
    }
    System.out.println("new String(data) = " + new String(data));
}
 
源代码13 项目: openjdk-jdk9   文件: RepeatedFormatReader.java
private static void test(final byte[] buffer)
        throws IOException, UnsupportedAudioFileException {
    final InputStream is = new ByteArrayInputStream(buffer);
    for (int i = 0; i < 10; ++i) {
        AudioSystem.getAudioFileFormat(is);
    }
}
 
源代码14 项目: openjdk-jdk9   文件: RecognizeHugeAiffFiles.java
/**
 * Tests the {@code AudioFileFormat} fetched from the fake header.
 * <p>
 * Note that the frameLength and byteLength are stored as int which means
 * that {@code AudioFileFormat} will store the data above {@code MAX_INT} as
 * NOT_SPECIFIED.
 */
private static void testAFF(final byte bits, final int rate,
                            final int channel, final long frameLength)
        throws Exception {
    final byte[] header = createHeader(bits, rate, channel, frameLength);
    final ByteArrayInputStream fake = new ByteArrayInputStream(header);
    final AudioFileFormat aff = AudioSystem.getAudioFileFormat(fake);

    if (aff.getType() != AudioFileFormat.Type.AIFF) {
        throw new RuntimeException("Error");
    }

    if (frameLength <= Integer.MAX_VALUE) {
        if (aff.getFrameLength() != frameLength) {
            System.err.println("Expected: " + frameLength);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    } else {
        if (aff.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
            System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    }
    validateFormat(bits, rate, channel, aff.getFormat());
}
 
源代码15 项目: openjdk-jdk9   文件: Aiff12bit.java
public static void test(byte[] file) throws Exception {
    InputStream inputStream = new ByteArrayInputStream(file);
    AudioFileFormat aff = AudioSystem.getAudioFileFormat(inputStream);

    if (aff.getFormat().getSampleSizeInBits() != 12) {
        throw new Exception("Wrong sample size. test FAILED");
    }
    if (aff.getFormat().getFrameSize() != 2) {
        throw new Exception("Wrong frame size. test FAILED");
    }
    if (aff.getFrameLength() != 100) {
        throw new Exception("Wrong file length. test FAILED");
    }
}
 
源代码16 项目: openjdk-jdk9   文件: AuZeroLength.java
public static void test(byte[] file) throws Exception {
    InputStream inputStream = new ByteArrayInputStream(file);
    AudioFileFormat aff = AudioSystem.getAudioFileFormat(inputStream);

    if (aff.getFrameLength() != 0) {
        throw new Exception("File length is "+aff.getFrameLength()+" instead of 0. test FAILED");
    }
    System.out.println(aff.getType()+" file length is 0.");
}
 
源代码17 项目: jdk8u-jdk   文件: ReadersExceptions.java
private static void test(final byte[] buffer) throws IOException {
    final InputStream is = new ByteArrayInputStream(buffer);
    try {
        AudioSystem.getAudioFileFormat(is);
    } catch (UnsupportedAudioFileException ignored) {
        // Expected.
        return;
    }
    throw new RuntimeException("Test Failed");
}
 
源代码18 项目: DTMF-Decoder   文件: PlayerTest.java
public void testPlayFile()
{
 try
 {
	if (out != null) out.println("---  Start : "+filename+"  ---");
	File file = new File(filename);
	AudioFileFormat aff = AudioSystem.getAudioFileFormat(file);
	if (out != null) out.println("Audio Type : "+aff.getType());
	AudioInputStream in= AudioSystem.getAudioInputStream(file);
	AudioInputStream din = null;
	if (in != null)
	{
	  AudioFormat baseFormat = in.getFormat();
	  if (out != null) out.println("Source Format : "+baseFormat.toString());
	  AudioFormat  decodedFormat = new AudioFormat(
		  AudioFormat.Encoding.PCM_SIGNED,
		  baseFormat.getSampleRate(),
		  16,
		  baseFormat.getChannels(),
		  baseFormat.getChannels() * 2,
		  baseFormat.getSampleRate(),
		  false);
	  if (out != null) out.println("Target Format : "+decodedFormat.toString());
	  din = AudioSystem.getAudioInputStream(decodedFormat, in);
	  if (din instanceof PropertiesContainer)
	  {
		assertTrue("PropertiesContainer : OK",true);
	  }
	  else
	  {
		assertTrue("Wrong PropertiesContainer instance",false);
	  }
	  rawplay(decodedFormat, din);
	  in.close();		
	  if (out != null) out.println("---  Stop : "+filename+"  ---");
	  assertTrue("testPlay : OK",true);
	}
 }
 catch (Exception e)
 {
	assertTrue("testPlay : "+e.getMessage(),false);
 }
}
 
源代码19 项目: openjdk-jdk9   文件: RecognizeHugeAuFiles.java
/**
 * Tests the {@code AudioFileFormat} fetched from the fake header.
 * <p>
 * Note that the frameLength and byteLength are stored as int which means
 * that {@code AudioFileFormat} will store the data above {@code  MAX_INT}
 * as NOT_SPECIFIED.
 */
private static void testAFF(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 AudioFileFormat aff = AudioSystem.getAudioFileFormat(fake);
    final AudioFormat format = aff.getFormat();

    if (aff.getType() != AudioFileFormat.Type.AU) {
        throw new RuntimeException("Error");
    }

    final long frameLength = size / format.getFrameSize();
    if (size != MAX_UNSIGNED_INT && frameLength <= Integer.MAX_VALUE) {
        if (aff.getFrameLength() != frameLength) {
            System.err.println("Expected: " + frameLength);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    } else {
        if (aff.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
            System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
            System.err.println("Actual: " + aff.getFrameLength());
            throw new RuntimeException();
        }
    }

    final long byteLength = size + AU_HEADER;
    if (byteLength <= Integer.MAX_VALUE) {
        if (aff.getByteLength() != byteLength) {
            System.err.println("Expected: " + byteLength);
            System.err.println("Actual: " + aff.getByteLength());
            throw new RuntimeException();
        }
    } else {
        if (aff.getByteLength() != AudioSystem.NOT_SPECIFIED) {
            System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
            System.err.println("Actual: " + aff.getByteLength());
            throw new RuntimeException();
        }
    }
    validateFormat(type[1], rate, channel, aff.getFormat());
}
 
源代码20 项目: gama   文件: BasicPlayer.java
/**
 * Inits Audio ressources from URL.
 */
protected void initAudioInputStream(final URL url) throws UnsupportedAudioFileException, IOException {
	m_audioInputStream = AudioSystem.getAudioInputStream(url);
	m_audioFileFormat = AudioSystem.getAudioFileFormat(url);
}