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

          常用鏈接

          留言簿(14)

          隨筆檔案(6)

          文章分類(467)

          文章檔案(423)

          相冊

          收藏夾(18)

          JAVA

          搜索

          •  

          積分與排名

          • 積分 - 827199
          • 排名 - 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 學習
          樓主好厲害,還是封裝功能本身比較好,其它的實現功能是否有副作用,不過封裝如果不好也不知道會怎么樣,自己實現要想清楚邏輯.  回復  更多評論
            
          主站蜘蛛池模板: 东丽区| 淮阳县| 麻江县| 波密县| 抚宁县| 壤塘县| 江山市| 托克逊县| 富锦市| 白朗县| 夏河县| 甘泉县| 札达县| 花垣县| 宝兴县| 东明县| 芜湖县| 昌图县| 屯门区| 德钦县| 元谋县| 崇仁县| 湖州市| 澄城县| 松溪县| 娱乐| 应城市| 迁安市| 长沙县| 新营市| 昌乐县| 庆阳市| 延吉市| 易门县| 哈巴河县| 泰和县| 许昌市| 黔西县| 美姑县| 安西县| 屏东县|