隨筆 - 6  文章 - 129  trackbacks - 0
          <2014年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(14)

          隨筆檔案(6)

          文章分類(467)

          文章檔案(423)

          相冊

          收藏夾(18)

          JAVA

          搜索

          •  

          積分與排名

          • 積分 - 827168
          • 排名 - 49

          最新評論

          閱讀排行榜

          評論排行榜

          文件來源:http://www.aygfsteel.com/beansoft/archive/2007/06/27/126550.html
          /*

           * @(#)AudioPlayer.java 1.0 03/04/17
           *
           
          */
          import java.awt.*;
          import java.awt.event.*;
          import java.applet.Applet;
          import java.applet.AudioClip;
          //{{{ Declares!
          class test {
          }
          //}}}
          public class AudioPlayer
              
          extends Frame {
            AudioClip clip 
          = null;
            FileDialog dialog 
          = new FileDialog(this);
            Label labelCopyright 
          = new Label();
            TextField textFieldFileName 
          = new TextField();
            Panel panel1 
          = new Panel();
            Button buttonOpen 
          = new Button();
            Button buttonPlay 
          = new Button();
            Button buttonLoop 
          = new Button();
            Button buttonStop 
          = new Button();

            
          public AudioPlayer() {
              setTitle(
          "AudioPlayer");
              addWindowListener(
          new WindowAdapter() {
                
          public void windowClosing(WindowEvent e) {
                  dispose();
                  System.exit(
          0);
                }
              }
              );
              
          try {
                jbInit();
              }
              
          catch (Exception e) {
                e.printStackTrace();
              }
              pack();
              setVisible(
          true);
            }

            
          private void jbInit() throws Exception {
              labelCopyright.setBackground(Color.darkGray);
              labelCopyright.setForeground(Color.orange);
              labelCopyright.setText(
                  
          "Copyright (c) 2003 BeanSoft Studio. All rights reserved.");
              
          this.setTitle("Java Audio Player");
              textFieldFileName.setEditable(
          false);
              buttonOpen.setLabel(
          "打開");
              buttonOpen.addActionListener(
          new java.awt.event.ActionListener() {
                
          public void actionPerformed(ActionEvent e) {
                  buttonOpen_actionPerformed(e);
                }
              }
              );
              buttonPlay.setLabel(
          "播放");
              buttonPlay.addActionListener(
          new java.awt.event.ActionListener() {
                
          public void actionPerformed(ActionEvent e) {
                  play();
                }
              }
              );
              buttonLoop.setLabel(
          "循環");
              buttonLoop.addActionListener(
          new java.awt.event.ActionListener() {
                
          public void actionPerformed(ActionEvent e) {
                  loop();
                }
              }
              );
              buttonStop.setLabel(
          "停止");
              buttonStop.addActionListener(
          new java.awt.event.ActionListener() {
                
          public void actionPerformed(ActionEvent e) {
                  stop();
                }
              }
              );
              
          this.add(labelCopyright, BorderLayout.SOUTH);
              
          this.add(textFieldFileName, BorderLayout.NORTH);
              
          this.add(panel1, BorderLayout.CENTER);
              panel1.add(buttonOpen, 
          null);
              panel1.add(buttonPlay, 
          null);
              panel1.add(buttonLoop, 
          null);
              panel1.add(buttonStop, 
          null);
            }

            
          public AudioClip getAudioClip() {
              
          return this.clip;
            }

            
          public void setAudioClip(AudioClip clip) {
              
          this.clip = clip;
            }

            
          public static void main(String args[]) {
              
          new AudioPlayer();
            }

            
          void buttonOpen_actionPerformed(ActionEvent e) {
              stop();
              dialog.show();
              
          if (dialog.getFile() != null) {
                String filename 
          = dialog.getDirectory() + dialog.getFile();
                
          try {
                  setAudioClip(Applet.newAudioClip( (
          new java.io.File(filename)).toURL()));
                }
                
          catch (Exception ex) {
                  ex.printStackTrace();
                }

                textFieldFileName.setText(filename);
              }
              play();
            }

            
          void play() {
              
          if (getAudioClip() != null) {
                getAudioClip().play();
              }
            }

            
          void loop() {
              
          if (getAudioClip() != null) {
                getAudioClip().loop();
              }
            }

            
          void stop() {
              
          if (getAudioClip() != null) {
                getAudioClip().stop();
              }
            }
          }


          posted on 2009-08-06 20:18 Ke 閱讀(1896) 評論(2)  編輯  收藏 所屬分類: java

          FeedBack:
          # re: AWT 練習 - 播放聲音文件 AudioPlayer.java(轉) 2013-03-03 22:12 zjut-jiangnan
          謝謝你的代碼,找了好久,看到完整的,能運行的。
          解決了我的Java swing聲音播放問題。  回復  更多評論
            
          # re: AWT 練習 - 播放聲音文件 AudioPlayer.java(轉) 2014-07-24 15:58 學習
          樓主好厲害,還是封裝功能本身比較好,其它的實現功能是否有副作用,不過封裝如果不好也不知道會怎么樣,自己實現要想清楚邏輯.  回復  更多評論
            
          主站蜘蛛池模板: 浙江省| 望城县| 太仆寺旗| 筠连县| 綦江县| 西贡区| 镇赉县| 东山县| 新源县| 外汇| 涿州市| 鄂尔多斯市| 阿克陶县| 宽城| 临西县| 崇信县| 昆明市| 宁远县| 虞城县| 东兰县| 克东县| 西乡县| 茶陵县| 阜宁县| 大关县| 泸州市| 准格尔旗| 织金县| 依安县| 灵山县| 兴和县| 永州市| 英超| 兴化市| 婺源县| 清水县| 南投市| 彩票| 遵化市| 彰武县| 九江县|