隨筆 - 6  文章 - 129  trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(14)

          隨筆檔案(6)

          文章分類(467)

          文章檔案(423)

          相冊

          收藏夾(18)

          JAVA

          搜索

          •  

          積分與排名

          • 積分 - 827222
          • 排名 - 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 閱讀(1897) 評論(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 學習
          樓主好厲害,還是封裝功能本身比較好,其它的實現功能是否有副作用,不過封裝如果不好也不知道會怎么樣,自己實現要想清楚邏輯.  回復  更多評論
            
          主站蜘蛛池模板: 花莲县| 青神县| 武冈市| 龙游县| 晴隆县| 邢台县| 同江市| 曲阜市| 漳浦县| 仁化县| 微山县| 德兴市| 曲靖市| 南充市| 拉萨市| 孟连| 奉节县| 乳山市| 吉林省| 大城县| 施秉县| 荣昌县| 淮安市| 三江| 阿坝| 宜良县| 新巴尔虎左旗| 瓦房店市| 道真| 清流县| 陕西省| 万载县| 青田县| 梧州市| 静宁县| 汉川市| 平武县| 汶川县| 南岸区| 翁源县| 诏安县|