Java by API/javax.sound.sampled/AudioFileFormat

Материал из Java эксперт
Версия от 14:41, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

AudioFileFormat.Type.AIFF

  

import java.io.File;
import java.net.URL;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioSystem;
public class Main {
  public static void main(String[] argv) throws Exception {
    AudioFileFormat fformat = AudioSystem.getAudioFileFormat(new File(
        "audiofile"));
    fformat = AudioSystem.getAudioFileFormat(new URL(
        "http://hostname/audiofile"));
    if (fformat.getType() == AudioFileFormat.Type.AIFC) {
    } else if (fformat.getType() == AudioFileFormat.Type.AIFF) {
    } else if (fformat.getType() == AudioFileFormat.Type.AU) {
    } else if (fformat.getType() == AudioFileFormat.Type.WAVE) {
    }
  }
}





AudioFileFormat.Type.AU

  

import java.io.File;
import java.net.URL;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioSystem;
public class Main {
  public static void main(String[] argv) throws Exception {
    AudioFileFormat fformat = AudioSystem.getAudioFileFormat(new File(
        "audiofile"));
    fformat = AudioSystem.getAudioFileFormat(new URL(
        "http://hostname/audiofile"));
    if (fformat.getType() == AudioFileFormat.Type.AIFC) {
    } else if (fformat.getType() == AudioFileFormat.Type.AIFF) {
    } else if (fformat.getType() == AudioFileFormat.Type.AU) {
    } else if (fformat.getType() == AudioFileFormat.Type.WAVE) {
    }
  }
}





AudioFileFormat.Type.WAVE

  

import java.io.File;
import java.net.URL;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioSystem;
public class Main {
  public static void main(String[] argv) throws Exception {
    AudioFileFormat fformat = AudioSystem.getAudioFileFormat(new File(
        "audiofile"));
    fformat = AudioSystem.getAudioFileFormat(new URL(
        "http://hostname/audiofile"));
    if (fformat.getType() == AudioFileFormat.Type.AIFC) {
    } else if (fformat.getType() == AudioFileFormat.Type.AIFF) {
    } else if (fformat.getType() == AudioFileFormat.Type.AU) {
    } else if (fformat.getType() == AudioFileFormat.Type.WAVE) {
    }
  }
}





AudioFormat.Encoding.PCM_SIGNED

  
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
public class Main {
  public static void main(String[] argv) throws Exception {
    AudioInputStream stream = AudioSystem.getAudioInputStream(new File(
        "audiofile"));
    // From URL
    // stream = AudioSystem.getAudioInputStream(new URL(
    // "http://hostname/audiofile"));
    AudioFormat format = stream.getFormat();
    if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
      format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format
          .getSampleRate(), format.getSampleSizeInBits() * 2, format
          .getChannels(), format.getFrameSize() * 2, format.getFrameRate(),
          true); // big endian
      stream = AudioSystem.getAudioInputStream(format, stream);
    }
    DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(),
        ((int) stream.getFrameLength() * format.getFrameSize()));
    Clip clip = (Clip) AudioSystem.getLine(info);
    clip.open(stream);
    clip.start();
  }
}