正文

关于Java中不调用Applet类播放声音的方法2006-04-16 17:00:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lovekker/12569.html

分享到:

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) {    }

阅读(3445) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册