随笔 - 6  文章 - 129  trackbacks - 0
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(14)

随笔档案(6)

文章分类(467)

文章档案(423)

相册

收藏夹(18)

JAVA

搜索

  •  

积分与排名

  • 积分 - 814160
  • 排名 - 49

最新评论

阅读排行榜

评论排行榜

文件來源:http://www.blogjava.net/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 阅读(1850) 评论(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 学习
楼主好厉害,还是封装功能本身比较好,其它的实现功能是否有副作用,不过封装如果不好也不知道会怎么样,自己实现要想清楚逻辑.  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: