e723. loading and playing sampled audio.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 40 行

TXT
40
字号
Supported audio file formats: aif, au, and wav. See also e724 Playing Streaming Sampled Audio. 
    try {
        // From file
        AudioInputStream stream = AudioSystem.getAudioInputStream(new File("audiofile"));
    
        // From URL
        stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile"));
    
        // At present, ALAW and ULAW encodings must be converted
        // to PCM_SIGNED before it can be played
        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);
        }
    
        // Create the clip
        DataLine.Info info = new DataLine.Info(
            Clip.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize()));
        Clip clip = (Clip) AudioSystem.getLine(info);
    
        // This method does not return until the audio file is completely loaded
        clip.open(stream);
    
        // Start playing
        clip.start();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    } catch (LineUnavailableException e) {
    } catch (UnsupportedAudioFileException e) {
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?