//************************************************************* // Java application // 「SoundTestApplication」 // //          作 成 者:ルート高菜    //          作成開始月:2006/5 //          最終更新月:2006/5 [TN082/J82] //************************************************************* import java.applet.*; import java.awt.*; import java.awt.event.*; import java.io.File; import java.net.*; import javax.swing.*; public class SoundTestApplication extends JFrame { //Eclipseの場合、シリアライズ可能クラスでこれがないと警告が出る private static final long serialVersionUID=0; // public static void main(String[] args) { SoundTestApplication frame=new SoundTestApplication(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //右上×印で終了 frame.setVisible(true); } //あらかじめ用意した音楽ファイル用 JComboBox soundList=new JComboBox(); JButton buttonPlay=new JButton("PLAY"); JButton buttonLoop=new JButton("LOOP"); JButton buttonStop=new JButton("STOP"); // AudioClip[] sounds; // //ファイルダイアログから選んで再生する用 FileDialog flDialog=new FileDialog(this); String dirName=System.getProperty("user.dir"); String fileName=""; // JButton buttonOpen2=new JButton("OPEN"); JButton buttonPlay2=new JButton("PLAY"); JButton buttonLoop2=new JButton("LOOP"); JButton buttonStop2=new JButton("STOP"); JLabel label2=new JLabel("< ---------------------------------------- >"); // AudioClip sound; //コンストラクタ public SoundTestApplication(){ // setTitle("SoundTestApplication"); setSize(400,150); //あらかじめ用意した音楽ファイル用 sounds=new AudioClip[4]; //取得にはApplet.newAudioClip()というstaticなメソッドを使う sounds[0]=Applet.newAudioClip(getClass().getResource("5X_DAYS.MID")); sounds[1]=Applet.newAudioClip(getClass().getResource("finalrain.au")); sounds[2]=Applet.newAudioClip(getClass().getResource("283.wav")); sounds[3]=Applet.newAudioClip(getClass().getResource("33333.mp3")); //sounds[3]=Applet.newAudioClip(SoundTestApplication.class.getResource("33333.mp3"));でもいい // soundList.addItem("5X_DAYS.MID"); soundList.addItem("finalrain.au"); soundList.addItem("283.wav"); soundList.addItem("33333.mp3"); // JPanel panel=new JPanel(); panel.add(soundList); panel.add(buttonPlay); panel.add(buttonLoop); panel.add(buttonStop); //ファイルダイアログから選んで再生する用 JPanel panel2=new JPanel(); panel2.setBackground(Color.WHITE); panel2.add(buttonOpen2); panel2.add(buttonPlay2); panel2.add(buttonLoop2); panel2.add(buttonStop2); panel2.add(label2); //コンテナ枠 Container cp=getContentPane(); cp.add(panel,BorderLayout.NORTH); cp.add(panel2,BorderLayout.CENTER); //ボタンイベント(あらかじめ用意した音楽ファイル用) buttonPlay.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ sounds[soundList.getSelectedIndex()].play(); soundList.setEnabled(false); buttonPlay.setEnabled(false); buttonLoop.setEnabled(false); buttonStop.setEnabled(true); } }); buttonLoop.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ sounds[soundList.getSelectedIndex()].loop(); soundList.setEnabled(false); buttonPlay.setEnabled(false); buttonLoop.setEnabled(false); buttonStop.setEnabled(true); } }); buttonStop.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ sounds[soundList.getSelectedIndex()].stop(); soundList.setEnabled(true); buttonPlay.setEnabled(true); buttonLoop.setEnabled(true); buttonStop.setEnabled(false); } }); //ボタンイベント(ファイルダイアログから選んで再生する用) buttonOpen2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ flDialog.setMode(FileDialog.LOAD); flDialog.setTitle("音楽ファイルを選択"); flDialog.setDirectory(dirName); flDialog.setFile(fileName); flDialog.setVisible(true); dirName=flDialog.getDirectory(); fileName=flDialog.getFile(); // if(fileName!=null){ try{ //ローカルファイルとしてのURLを作る File file=new File(dirName+fileName); sound=Applet.newAudioClip(file.toURL()); }catch(MalformedURLException exception){ JOptionPane.showMessageDialog(SoundTestApplication.this,"読み込みに失敗しました。","エラー",JOptionPane.ERROR_MESSAGE); return; } label2.setText("< "+dirName+fileName+" >"); buttonPlay2.setEnabled(true); buttonLoop2.setEnabled(true); buttonStop2.setEnabled(false); } } }); buttonPlay2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ sound.play(); buttonOpen2.setEnabled(false); buttonPlay2.setEnabled(false); buttonLoop2.setEnabled(false); buttonStop2.setEnabled(true); } }); buttonLoop2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ sound.loop(); buttonOpen2.setEnabled(false); buttonPlay2.setEnabled(false); buttonLoop2.setEnabled(false); buttonStop2.setEnabled(true); } }); buttonStop2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ sound.stop(); buttonOpen2.setEnabled(true); buttonPlay2.setEnabled(true); buttonLoop2.setEnabled(true); buttonStop2.setEnabled(false); } }); //ボタンの有効無効(初期値) buttonStop.setEnabled(false); // buttonPlay2.setEnabled(false); buttonLoop2.setEnabled(false); buttonStop2.setEnabled(false); } }