【機能概要】
サイト上にあるサウンドファイルをSourceDataLineで再生
(オーディオデータをリアルタイムにストリーミングしながら再生する)
【ソース】
package com.naozary.test.sound;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.Formatter;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.swing.JApplet;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class SoundTest02 extends JApplet implements ActionListener, Runnable {
// JPanel jPanel1 = new JPanel();
JScrollPane jPanel1 = null;
JTextArea tArea = new JTextArea();
Dimension dim = null;
Thread thread = null;
public void init() {
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
updateLayout();
}
});
dim = getSize();
// System.out.printf("%d , %d",dim.width,dim.height );
tArea.setEditable(false);
// this.setSize(new Dimension(550,300));
this.setSize(dim);
// jPanel1.setLayout(null);
tArea.setBounds(new Rectangle(0, 0, this.getWidth(), this.getHeight()));
// tArea.setText("AAAAA"); //デフォルトのテキストを表示
// tArea.setFont(new Font("Serif",Font.PLAIN,16));
tArea.setFont(new Font("Monospaced", Font.PLAIN, 16));
tArea.setTabSize(4); // タブサイズを設定
tArea.setForeground(Color.white);
tArea.setBackground(Color.black);
tArea.setLineWrap(true); // 右辺で自動折り返しにする
tArea.setSelectionColor(Color.lightGray); // 反転選択範囲の背景色
tArea.setSelectedTextColor(Color.blue); // 反転選択範囲の文字色
// jPanel1.add(tArea);
jPanel1 = new JScrollPane(tArea);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
AppletOn = true;
thread = new Thread(this);
thread.start();
}
public void run() {
// case001();
case009();
}
public void actionPerformed(ActionEvent e) {
}
public void updateLayout() {
// System.out.printf("*** enter updateLayout() ***\n" );
dim = getSize();
this.setSize(dim);
tArea.setBounds(new Rectangle(0, 0, this.getWidth(), this.getHeight()));
}
StringBuilder builder = new StringBuilder();
Formatter formatter = new Formatter(builder);
boolean AppletOn = false;
public void printf(String arg0, Object... args) {
formatter.format(arg0, args);
if (AppletOn) {
tArea.append(formatter.out().toString()); // デフォルトのテキストを表示
} else {
System.out.print(formatter.out());
}
// System.out.println("builder.length() >>>" + builder.length());
builder.delete(0, builder.length());
}
public void case009() {
printf("PLAY SOUND \n");
FileOutputStream fos = null;
DataInputStream dis = null;
byte[] buffer = new byte[8000];
int off, len, l;
AudioFormat.Encoding enc = new AudioFormat.Encoding("PCM_SIGNED");
AudioFormat format2 = new AudioFormat(
enc,
22050.0F,
16,
2,
4,
22050.0F,
false
);
SourceDataLine line = null;
try {
// DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class,format);
DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class,format2);
line = (SourceDataLine) AudioSystem.getLine(lineInfo);
// line.open(format);
line.open(format2);
line.start();
URL url = new URL("http://www.naozary.com/download?registerno=20100405193321564");
dis = new DataInputStream(
url.openStream()
);
while ((len = dis.read(buffer)) > 0) {
for (off = 0; len > 0; off += l, len -= l) {
l = line.write(buffer, off, len);
}
}
} catch (Exception e) {
printf("%s\n", e.toString());
} finally {
if (line != null)line.close();
if (dis != null){
try{ dis.close(); }catch(Exception e){}
}
// System.exit(0);
}
}
public static void main(String[] args) {
SoundTest02 t01 = new SoundTest02();
// t01.case001();
// t01.case002();
// t01.case003();
// t01.case004();
// t01.case005();
// t01.case006();
// t01.case007();
// t01.case008();
t01.case009();
}
}
【実行結果】
別ウィンドウで表示
0 件のコメント:
コメントを投稿