下面列出了javax.sound.sampled.AudioSystem#getAudioFileFormat ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* {@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;
}
/**
* 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;
}
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");
}
}
/**
* 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;
}
/**
* @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;
}
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");
}
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");
}
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");
}
/**
* 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());
}
/**
* 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());
}
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");
}
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));
}
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);
}
}
/**
* 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());
}
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");
}
}
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.");
}
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");
}
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);
}
}
/**
* 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());
}
/**
* Inits Audio ressources from URL.
*/
protected void initAudioInputStream(final URL url) throws UnsupportedAudioFileException, IOException {
m_audioInputStream = AudioSystem.getAudioInputStream(url);
m_audioFileFormat = AudioSystem.getAudioFileFormat(url);
}