Swing/Sound のバックアップの現在との差分(No.14)
TITLE:Wavファイルの演奏
Wavファイルの演奏
編集者:Terai Atsuhiro~
作成日:2004-08-16
更新日:2021-03-14 (日) 19:51:22
概要
AudioSystem
を使って、AudioInputStream
、Clip
(Line
)などを作成取得し、wav
ファイルを演奏します。
Screenshot
Advertisement
概要
AudioSystemを使って、AudioInputStream、Clip(Line)などを作成取得し、wavファイルを演奏します。#screenshot
サンプルコード
#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
URL url = getClass().getResource("notice1.wav");
#spandel
AudioInputStream sound = AudioSystem.getAudioInputStream(url);
#spanend
#spandel
AudioFormat format = sound.getFormat();
#spanend
#spandel
DataLine.Info di = new DataLine.Info(Clip.class, format);
#spanend
#spandel
Clip clip = (Clip) AudioSystem.getLine(di);
#spanend
#spandel
clip.open(sound);
#spanend
#spandel
clip.start();
#spanend
#spanadd
try (AudioInputStream sound = AudioSystem.getAudioInputStream(url);
#spanend
Clip clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, sound.getFormat()))) {
SecondaryLoop loop = Toolkit.getDefaultToolkit().getSystemEventQueue().createSecondaryLoop();
clip.addLineListener(e -> {
LineEvent.Type t = e.getType();
if (Objects.equals(t, LineEvent.Type.STOP) || Objects.equals(t, LineEvent.Type.CLOSE)) {
loop.exit();
}
});
clip.open(sound);
clip.start();
loop.enter();
#spanadd
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
#spanend
ex.printStackTrace();
Toolkit.getDefaultToolkit().beep();
#spanadd
}
#spanend
- &jnlp;
- &jar;
- &zip;
解説
WAVE、AU、AIFF、SND形式のサウンドファイルが演奏できるようです。解説
AudioSystem
を使用すれば、WAVE
、AU
、AIFF
、SND
形式のサウンドファイルが演奏可能です。
参考リンク
- "taitai studio" フリーWav素材集
- 上記のサイトのwavファイルをサンプルとして利用しています。
-
AudioInputStream
やClip
をAutoCloseable
で自動的に開放するためtry-with-resources
ブロックで作成 - 演奏開始後に
SecondaryLoop
で現在のスレッドをブロックし新しいセカンダリイベントループで演奏を継続 - 演奏終了イベントを
LineListener
で受け取るとセカンダリイベントループを終了して現在のスレッドに戻りclose()
が自動的に実行される
コメント
- fun search test!http://fun-things.info/christmas-gifts/christmas-gifts-for-guy.html<a href="http://fun-things.info/christmas-gifts/christmas-gift-basket-making-ideas.html">christmas gift basket making ideas</a><a href="http://fun-things.info/christmas-cards/greeting-cards-for-christmas.html">greeting cards for christmas</a>http://fun-things.info/christmas-gifts/basket-candle-christmas-gift.htmlhttp://fun-things.info/christmas-cards/personlized-christmas-cards-with-gifts.html<a href="http://fun-things.info/woman-christmas-gift-ideas.html">woman christmas gift ideas</a><a href="http://fun-things.info/christmas-cards/sports-christmas-card.html">sports christmas card</a><a href="http://fun-things.info/christmas-gifts/websites-with-christmas-gift-ideas.html">websites with christmas gift ideas</a>http://fun-things.info/christmas-gifts/christmas-gifts-for-ladies.htmlhttp://fun-things.info/christmas-cards/anne-geddes-christmas-card.html<a href="http://fun-things.info/christmas-gifts/christmas-gifts-for-parents.html">christmas gifts for parents</a><a href="http://fun-things.info/christmas-cards/christmas-card-chocolate-mold.html">christmas card chocolate mold</a>http://fun-things.info/christmas-cards/gifts-and-collectibles-christmas-gift-cards-search3-fashion.htmlhttp://fun-things.info/christmas-cards/charity-christmas-card.htmlhttp://fun-things.info/christmas-gifts/boys-christmas-gift-ideas.html -- boxfullo?
参考リンク
- AudioSystem (Java Platform SE 8)
-
"taitai studio" フリーWav素材集-
上記のサイトのサイトが移転しているようなので、別の素材を探す予定wav
ファイルをサンプルとして利用しています。
-
- MIDIファイルの演奏
- Beep音を鳴らす
- AuditoryCuesでイベント音を設定する