import java.applet.Applet;
10年積累的成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有宜興免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
import java.applet.AudioClip;
import java.awt.AWTException;
import java.awt.Frame;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class bofan_2 extends JFrame implements ActionListener
{
boolean looping=false;
File file1=null;
AudioClip sound1;
AudioClip chosenClip;
private JComboBox box1=null; //歌曲列表
private JButton butbofan=null; //播放
private JButton butboxhuan=null; //循環(huán)播放
private JButton buttinzi=null; //停止
private JButton butshan=null; //上一首
private JButton butzhantin=null; //暫停
private JButton butxia=null; //下一首
private TrayIcon trayIcon;//托盤圖標(biāo)
private SystemTray systemTray;//系統(tǒng)托盤
public bofan_2()
{
this.setSize(420,400);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setLayout(null);
box1=new JComboBox();
box1.addItem("傷心太平洋");
box1.addItem("勁爆的士高");
box1.addItem("老夫少妻");
box1.addItem("愛不再來");
box1.addItem("抽身");
box1.addItem("傷心城市");
box1.addItem("二零一二");
box1.addItem("精忠報(bào)國");
box1.addItem("秋沙");
box1.addItem("吻別");
box1.addItem("音樂瘋起來");
box1.setBounds(10,20,150,20);
butbofan=new JButton("播放");
butbofan.addActionListener(this);
butbofan.setBounds(165,50,60,20);
butboxhuan=new JButton("循環(huán)播放");
butboxhuan.addActionListener(this);
butboxhuan.setBounds(230,50,90,20);
buttinzi=new JButton("停止");
buttinzi.setEnabled(false);
buttinzi.addActionListener(this);
buttinzi.setBounds(335,50,60,20);
butshan=new JButton("上一首");
butshan.addActionListener(this);
butshan.setBounds(165,90,80,20);
butzhantin=new JButton("暫停");
butzhantin.setEnabled(false);
butzhantin.addActionListener(this);
butzhantin.setBounds(250,90,60,20);
butxia=new JButton("下一首");
butxia.addActionListener(this);
butxia.setBounds(320,90,80,20);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(box1);
this.getContentPane().add(butbofan);
this.getContentPane().add(butboxhuan);
this.getContentPane().add(buttinzi);
this.getContentPane().add(butshan);
this.getContentPane().add(butzhantin);
this.getContentPane().add(butxia);
try {
UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceOfficeBlue2007LookAndFeel");
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (InstantiationException e)
{
e.printStackTrace();
} catch (IllegalAccessException e)
{
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e)
{
e.printStackTrace();
}
setSize(450,450);
systemTray = SystemTray.getSystemTray();//獲得系統(tǒng)托盤的實(shí)例
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
trayIcon = new TrayIcon(ImageIO.read(new File("004.jpg")));
systemTray.add(trayIcon);//設(shè)置托盤的圖標(biāo),0.gif與該類文件同一目錄
}
catch (IOException e1)
{
e1.printStackTrace();
}
catch (AWTException e2)
{
e2.printStackTrace();
}
this.addWindowListener(
new WindowAdapter(){
public void windowIconified(WindowEvent e)
{
dispose();//窗口最小化時(shí)dispose該窗口
}
});
trayIcon.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 2)//雙擊托盤窗口再現(xiàn)
setExtendedState(Frame.NORMAL);
setVisible(true);
}
});
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source== butbofan)
{
System.out.println((String) box1.getSelectedItem());
file1=new File((String) box1.getSelectedItem()+".wav");
butboxhuan.setEnabled(true);
buttinzi.setEnabled(true);
butzhantin.setEnabled(true);
butzhantin.setText("暫停");
try {
sound1 = Applet.newAudioClip(file1.toURL());
chosenClip = sound1;
} catch(OutOfMemoryError er){
System.out.println("內(nèi)存溢出");
er.printStackTrace();
} catch(Exception ex){
ex.printStackTrace();
}
chosenClip.play();
this.setTitle("正在播放"+(String) box1.getSelectedItem());
}
if (source== butboxhuan)
{
file1=new File((String) box1.getSelectedItem()+".wav");
try {
sound1 = Applet.newAudioClip(file1.toURL());
chosenClip = sound1;
} catch(OutOfMemoryError er){
System.out.println("內(nèi)存溢出");
er.printStackTrace();
} catch(Exception ex){
ex.printStackTrace();
}
looping = true;
chosenClip.loop();
butboxhuan.setEnabled(false);
buttinzi.setEnabled(true);
butzhantin.setText("暫停");
this.setTitle("正在循環(huán)播放"+(String) box1.getSelectedItem());
}
if (source== buttinzi)
{
if (looping)
{
looping = false;
chosenClip.stop();
butboxhuan.setEnabled(true);
butzhantin.setText("暫停");
} else {
chosenClip.stop();
}
buttinzi.setEnabled(false);
this.setTitle("停止播放");
}
if(source==butshan)
{
butzhantin.setText("暫停");
}
if(source==butzhantin)
{
buttinzi.setEnabled(false);
butzhantin.setText("繼續(xù)");
if(source==butzhantin)
{
butzhantin.setText("暫停");
}
}
if(source==butxia)
{
butzhantin.setText("暫停");
}
}
public static void main(String[] args)
{
bofan_2 xx=new bofan_2();
}
}
/*
可以用加載聲音文件的方法:
第一幀:mysound= new Sound();
mysound.attachSound(聲音id名字);
ptime = 0;
播放按鈕as:
on(release){
mysound.start(ptime);
}
暫停按鈕as:
on(release){
ptime = mysound.position/1000;
mysound.stop();
}
*/
java swt實(shí)現(xiàn)播放音樂代碼如下:
public void play(String Filename)
{
try{
// 用輸入流打開一音頻文件
InputStream in = new FileInputStream(Filename);//FIlename 是你加載的聲音文件如(“game.wav”)
// 從輸入流中創(chuàng)建一個(gè)AudioStream對象
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);//用靜態(tài)成員player.start播放音樂
//AudioPlayer.player.stop(as);//關(guān)閉音樂播放
//如果要實(shí)現(xiàn)循環(huán)播放,則用下面的三句取代上面的“AudioPlayer.player.start(as);”這句
/*AudioData data = as.getData();
ContinuousAudioDataStream gg= new ContinuousAudioDataStream (data);
AudioPlayer.player.start(gg);// Play audio.
*/
//如果要用一個(gè) URL 做為聲音流的源(source),則用下面的代碼所示替換輸入流來創(chuàng)建聲音流:
/*AudioStream as = new AudioStream (url.openStream());
*/
} catch(FileNotFoundException e){
System.out.print("FileNotFoundException ");
} catch(IOException e){
System.out.print("有錯(cuò)誤!");
}
}
我?guī)湍惆殉绦蚋耐炅?,可以?shí)現(xiàn)用按鈕來控制音樂播放與停止了,你看看吧。
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java點(diǎn)虐 .URI;
import java點(diǎn)虐 .URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class f extends JFrame implements ActionListener {
private static final String AudioClip = null;
private JButton Oj;
private JButton Oj1;
AudioClip clip =null;
File musicFile;
URI uri;
URL url;
private void f() throws InterruptedException{
musicFile = new File("E:\\JAVA\\new1\\celine dion - falling into you.wav");
uri = musicFile.toURI();
try {
url = uri.toURL();
} catch (Exception e) {
}
clip=Applet.newAudioClip(url);
//clip.play();
}
f() {
this.setSize(800, 600);
this.setResizable(false);
JPanel p = new JPanel();
this.setContentPane(p);
this.setVisible(true);
Oj = new JButton("開始");
Oj1 = new JButton("結(jié)束");
this.setVisible(true);
Oj.addActionListener(this);
Oj1.addActionListener(this);
this.add(Oj);
this.add(Oj1);
}
public static void main(String[] args) throws InterruptedException {
f ff=new f();
ff.f();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == Oj) {
System.out.println("進(jìn)入游戲界面");
play();
} else if (e.getSource() == Oj1) {
System.out.println("退出游戲");
stop();
//System.exit(0);
}
}
public void play() {
if (clip != null)
( (java.applet.AudioClip) clip).play();
}
public void stop() {
if (clip != null)
( (java.applet.AudioClip) clip).stop();
}
}
你都已經(jīng)寫出大半了,再努力努力!
已經(jīng)播放的那首歌曲,代碼是怎么寫的? 下一首照舊。
監(jiān)聽器中無非是做兩件事,更新視圖,指向下一首。后臺(tái)讀取下一首歌曲的文件到內(nèi)存,然后播放。。。還是停在上一首,可能是內(nèi)存沒更新,清空,再讀取。
我還是建議你自己加油。 別人把代碼給你了,你還得研究別人的代碼風(fēng)格,不如以自己的代碼為基礎(chǔ)。