Created
May 8, 2014 03:57
-
-
Save 4noha/e61b387212bb875ff25a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this license header, choose License Headers in Project Properties. | |
| * To change this template file, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| import java.awt.BorderLayout; | |
| import java.awt.Dimension; | |
| import java.awt.FlowLayout; | |
| import java.awt.datatransfer.DataFlavor; | |
| import java.awt.datatransfer.UnsupportedFlavorException; | |
| import java.awt.dnd.DnDConstants; | |
| import java.awt.dnd.DropTarget; | |
| import java.awt.dnd.DropTargetAdapter; | |
| import java.awt.dnd.DropTargetDropEvent; | |
| import java.awt.event.ActionEvent; | |
| import java.awt.event.ActionListener; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.util.List; | |
| import java.util.logging.Level; | |
| import java.util.logging.Logger; | |
| import javax.sound.sampled.AudioFormat; | |
| import javax.sound.sampled.AudioInputStream; | |
| import javax.sound.sampled.AudioSystem; | |
| import javax.sound.sampled.Clip; | |
| import javax.sound.sampled.DataLine; | |
| import javax.sound.sampled.FloatControl; | |
| import javax.sound.sampled.LineEvent; | |
| import javax.sound.sampled.LineListener; | |
| import javax.sound.sampled.LineUnavailableException; | |
| import javax.sound.sampled.UnsupportedAudioFileException; | |
| import javax.swing.DefaultListModel; | |
| import javax.swing.JButton; | |
| import javax.swing.JFrame; | |
| import static javax.swing.JFrame.EXIT_ON_CLOSE; | |
| import javax.swing.JLabel; | |
| import javax.swing.JList; | |
| import javax.swing.JPanel; | |
| import javax.swing.JScrollPane; | |
| /** | |
| * | |
| * @author makiron | |
| * | |
| * ×もthorowされてるのか | |
| * | |
| * そういや曲削除ないな | |
| * | |
| * | |
| */ | |
| public class ReadMusic0 extends JFrame implements ActionListener, LineListener{ | |
| static Clip clip = null; | |
| static File soundFile = null; | |
| final static String STOP = "Stop"; | |
| // ボタン用テキスト 再生 | |
| final static String PLAY = "Play"; | |
| // ボタン用テキスト 一時停止 | |
| final static String PAUSE = "Pause"; | |
| // タイトル | |
| final static String TITLE = "BasicPlayerでmp3再生"; | |
| final static String FILE_NAME = "ファイル名:"; | |
| // 再生ボタン | |
| JButton play, stop, next, before; | |
| JLabel label; | |
| JScrollPane sp; | |
| DefaultListModel titleList; | |
| List<File> musicList; | |
| int trackNumber = 0; | |
| boolean running = false; | |
| protected JList list; | |
| //protected JTextField selectText; | |
| /* JListの初期データ */ | |
| //String[] initData = {}; | |
| public static void main(String[] args) { | |
| // TODO code application logic here | |
| new ReadMusic0();//コンストラクタ呼び出し | |
| } | |
| public ReadMusic0() { | |
| // コンストラクタ | |
| //setTitle(TITLE); | |
| setDefaultCloseOperation(EXIT_ON_CLOSE); | |
| setBounds(0, 0, 700, 300);//300,150 | |
| setLayout(new FlowLayout()); | |
| /* JListの初期データ */ | |
| //String[] initData = {"music0", "1", "2", "3", "4"};//どろっぷされてから入れる | |
| //initData = new String[]{"music0"}; | |
| titleList = new DefaultListModel(); | |
| //list = new JList(initData); | |
| //for(String mName : initData){ | |
| // musicList.addElement(mName); | |
| //} | |
| list = new JList(titleList); | |
| sp = new JScrollPane(); | |
| sp.getViewport().setView(list);//ここでセットしちゃったら後で書き換えられないしセットし直さないといけない? | |
| sp.setPreferredSize(new Dimension(200, 270)); | |
| //パネルいつはりつけてるー?? | |
| JPanel p = new JPanel(); | |
| p.add(sp); | |
| getContentPane().add(p, BorderLayout.WEST); | |
| //selectText = new JTextField(1); | |
| // ドロップターゲット設定 | |
| new DropTarget(this, DnDConstants.ACTION_COPY, | |
| new MyDropTargetListener()); | |
| // 再生、停止ボタン設置 | |
| play = new JButton(PLAY); | |
| play.addActionListener(this);//このthisようわからん適当にキャストすればええもんなん? | |
| //bPlay.addActionListener(new bPlayAction()); | |
| stop = new JButton(STOP); | |
| //bStop.addActionListener(new bStopAction()); | |
| next = new JButton("Next"); | |
| before = new JButton("Before"); | |
| next.addActionListener(this); | |
| before.addActionListener(this); | |
| //コマンドとして登録する | |
| //open.setActionCommand("open"); | |
| //反映させる | |
| //open.addActionListener(this); | |
| add(before); | |
| add(play); | |
| add(stop); | |
| add(next); | |
| // ラベル設置 | |
| label = new JLabel("wavファイルをドロップしてください。"); | |
| //label.setPreferredSize(new Dimension(getWidth() - 10, 20)); | |
| //this.add(label); | |
| setLayout(new FlowLayout()); | |
| add(label, BorderLayout.SOUTH); | |
| //add(getHr(2000, 0)); | |
| //いらないーw | |
| // BasicPlayerのインスタンス作成 | |
| //player = new BasicPlayer(); | |
| getContentPane().add(p);//の意味って? | |
| this.setResizable(false);//サイズ固定 | |
| setVisible(true); | |
| } | |
| //ボタンアクション | |
| public void actionPerformed(ActionEvent event) { | |
| String cmd = event.getActionCommand(); | |
| if (cmd.equals("Play") && musicList != null) { | |
| label.setText(FILE_NAME + musicList.get(0).getName());//なんで反映されてへんねんやろ | |
| testPlayClip(musicList.get(0));//でもできればリストで曲名クリックしたい | |
| } | |
| if (cmd.equals("Stop")) {//再生中に割り込みでボタンやってくれない・・・×ボタンもしかりorz | |
| //鳴ってるか分岐刷る必要ある? | |
| System.out.println("aaa"); | |
| stop(); | |
| } | |
| if (cmd.equals("Next")) { | |
| trackNumber++; | |
| if (musicList.get(trackNumber) == null) { | |
| testPlayClip(musicList.get(0)); | |
| } else { | |
| testPlayClip(musicList.get(trackNumber)); | |
| } | |
| } | |
| if (cmd.equals("Before")) { | |
| trackNumber--; | |
| if (musicList.get(trackNumber) == null) { | |
| testPlayClip(musicList.get(0)); | |
| } else { | |
| testPlayClip(musicList.get(trackNumber)); | |
| } | |
| } | |
| } | |
| // @Override | |
| // public void actionPerformed(ActionEvent e) { | |
| // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. | |
| // } | |
| // ドロップターゲットリスナー | |
| // ドロップされたファイルを受け取り、最初のファイルだけを再生 | |
| /* | |
| * ドラッグアンドドロップのサンプル 1.ドロップを受け取るだけのサンプル(文字列、ファイル) | |
| * ポイント1.ドロップイベントを受け取るには「DropTargetListener.drop」を実装します。 | |
| * ポイント2.「DropTargetListener」は「DropTarget」に設定します。 | |
| * new DropTarget(コンポーネント, リスナー);を実行すると、DropTargetの内部でaddDropTargetListenerされます。 | |
| * ポイント3.ドロップすると1.で実装した「drop」が実行されます。 | |
| * 「DropTargetDropEvent.acceptDrop」でドロップを受け取る準備をします。 | |
| * 「DropTargetDropEvent.isDataFlavorSupported」で、受け取り可能なフレーバーを調べます。 | |
| * 標準では文字列用の「stringFlavor」、ファイル用の「javaFileListFlavor」、画像イメージ用の「imageFlavor」があります。 | |
| * 「DropTargetDropEvent.getTransferable」で転送クラスを取得します。 | |
| * 「Transferable.getTransferData」でドロップされたオブジェクトを受け取ります。 | |
| * ファイルは「List<File>」にキャストして操作するとよいです。 | |
| * 「DropTargetDropEvent.dropComplete」で転送完了を通知して終了です。 | |
| */ | |
| class MyDropTargetListener extends DropTargetAdapter { | |
| @Override | |
| public void drop(DropTargetDropEvent dtde) { | |
| dtde.acceptDrop(DnDConstants.ACTION_COPY); | |
| boolean b = false;//意味あるの? | |
| try { | |
| if (dtde.getTransferable().isDataFlavorSupported( | |
| DataFlavor.javaFileListFlavor)) { | |
| b = true; | |
| musicList = (List<File>) dtde.getTransferable() | |
| .getTransferData(DataFlavor.javaFileListFlavor); | |
| // 最初のファイルだけ取得 | |
| for (int i = 0; i < musicList.size(); i++) { | |
| File file = musicList.get(i); | |
| register(file); | |
| } | |
| //曲名はmusicListに入れたらいいけど本体は | |
| //複数同時むりか | |
| } | |
| } catch (UnsupportedFlavorException | IOException e) { | |
| } finally { | |
| dtde.dropComplete(b); | |
| } | |
| } | |
| } | |
| private void register(File file) { | |
| // ラベル設定やったのをListに設定したい | |
| //label.setText(FILE_NAME + file.getName()); | |
| // 取得したファイルを開く | |
| //player.open(file); | |
| //play(); // 再生 | |
| //String[] initData2 = Arrays.copyOf(initData, initData.length+1); | |
| //initData2[initData.length] = file.getName(); | |
| //list = new JList(initData2); | |
| //initData = initData2; | |
| titleList.addElement(file.getName()); | |
| //sp.getViewport().setView(list);s | |
| } | |
| public void testPlayClip(File file) { | |
| if (running) return; | |
| soundFile = new File(file.getPath()); | |
| AudioInputStream audioInputStream = null; | |
| if (null == soundFile) { | |
| return; | |
| } | |
| try { | |
| audioInputStream = AudioSystem.getAudioInputStream(soundFile); | |
| } catch (UnsupportedAudioFileException ex) { | |
| Logger.getLogger(ReadMusic0.class.getName()).log(Level.SEVERE, null, ex); | |
| } catch (IOException ex) { | |
| Logger.getLogger(ReadMusic0.class.getName()).log(Level.SEVERE, null, ex); | |
| } | |
| AudioFormat audioFormat = audioInputStream.getFormat(); | |
| DataLine.Info info = new DataLine.Info(Clip.class, audioFormat); | |
| try { | |
| clip = (Clip) AudioSystem.getLine(info); | |
| } catch (LineUnavailableException ex) { | |
| Logger.getLogger(ReadMusic0.class.getName()).log(Level.SEVERE, null, ex); | |
| } | |
| //javax.sound.sampled.FloatControlをClipから取得し、音量調節を行うことが できます。 | |
| //取得してこれるってことはその音量の値出せるの? | |
| /* | |
| for (int i = 0; i < 10000; i++) { | |
| //targetDataLine = TargetDataLine)AudioSystem.getLine(info); | |
| float x = clip.getLevel(); | |
| System.out.println(x); | |
| try { | |
| Thread.sleep(10); | |
| } catch (InterruptedException e) { | |
| } | |
| } | |
| */ | |
| //clip.loop(LOOP_CONTINUOUSLY); | |
| try { | |
| clip.open(audioInputStream); | |
| } catch (LineUnavailableException | IOException ex) { | |
| return; | |
| } | |
| FloatControl control = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); | |
| controlByLinearScalar(control,1.0); //2-0.0001 | |
| //while (clip.isRunning() && clip.isOpen()); | |
| if (clip.isRunning() == false && false == running) { | |
| clip.start(); | |
| clip.addLineListener((LineListener) this); | |
| running = true; | |
| } | |
| } | |
| public void update(LineEvent event) { | |
| if (event.getType() == LineEvent.Type.STOP) { | |
| clip.stop(); | |
| clip.close(); | |
| clip.setFramePosition(0); | |
| running = false; | |
| clip.removeLineListener((LineListener) this); | |
| } | |
| } | |
| public static void stop() { | |
| clip.stop(); | |
| } | |
| public static void controlByLinearScalar(FloatControl control, double linearScalar) { | |
| control.setValue((float) Math.log10(linearScalar) * 20); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment