2010年4月4日日曜日

サイト上にあるサウンドファイルをClipで再生

【機能概要】
サイト上にあるサウンドファイルをClipで再生
(オーディオデータを全部プリロードして再生する)



【ソース】
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.net.URL;
import java.util.Formatter;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.Line;
import javax.swing.JApplet;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class SoundTest01  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();
        case004();
    }
  
    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 case004(){
        printf("PLAY SOUND \n");
      
      
        Clip clip = null;
         try{
            
             URL url = new URL(
                "http://www.naozary.com/download?registerno=20100404181152490"
             );
            

             AudioInputStream as
                       = AudioSystem.getAudioInputStream(url.openStream());
           
             clip = (Clip)AudioSystem.getLine(
                                    new Line.Info(Clip.class));
            clip.open(as);
            clip.start();

            do{
                Thread.sleep(1000);
            }while(clip.isRunning());
         }
         catch(Exception e){
            
         }
         finally
         {
             if(clip != null) clip.close();

         }
      
    }

  
  
    public static void main(String[] args) {
        SoundTest01    t01 = new SoundTest01();
         t01.case004();
    }

}


【実行結果】


別ウィンドウで実行

0 件のコメント:

コメントを投稿