《java簡(jiǎn)單播放器源碼》由會(huì)員分享,可在線閱讀,更多相關(guān)《java簡(jiǎn)單播放器源碼(7頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、import java.awt.BorderLayout;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import
2、javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.Rectangle;
import javax.swing.JTable;
import java.awt.Dimension;
import javax.swing.JTextPane;
import jav
3、ax.swing.JScrollBar;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import .MalformedURLException;
import javax.swing.JButton;
import javax.swing.filechooser.FileNameExtensionFilter;
public
4、class Hello extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private List list = null;
private JButton jButton = null;
private Player play; // @jve:decl-index=0:
private String str = null;
priv
5、ate JButton jButton1 = null;
private JButton jButton2 = null;
private boolean b=false;
private static SourceDataLine objSDL;
private JButton jButton3 = null;
/**
* This is the default constructor
*/
public Hello() {
super();
ImageIcon backIcon = new ImageIcon(getClass().g
6、etResource(
"/image/hehe.jpg"));
final JLabel backLabel = new JLabel(backIcon); // 創(chuàng)建背景標(biāo)簽
backLabel.setSize(backIcon.getIconWidth(), backIcon.getIconHeight());
backLabel.setLocation(0, 0);
this.add(backLabel);
initialize();
}
/**
* This method initializes this
*
*
7、 @return void
*/
private void initialize() {
this.setSize(594, 346);
this.setContentPane(getJContentPane());
this.setTitle("達(dá)達(dá)播放器");
setVisible(true);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
// Start Output
/**
* 打
8、開(kāi)音頻設(shè)備.
* @param intFreq 采樣率。
* @param iBits PCM位深(例如16)。
* @param channels 聲道數(shù)
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getList(), null);
jContentPane.add(getJButton()
9、, null);
jContentPane.add(getJButton1(), null);
jContentPane.add(getJButton2(), null);
jContentPane.add(getJButton3(), null);
}
return jContentPane;
}
public void play(String str, List list) {
File ff = new File(str + list.getSelectedItem());
try {
play = Manager.cr
10、eatePlayer(ff.toURL());
} catch (NoPlayerException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
11、 e1.printStackTrace();
}
play.start();
}
/**
* This method initializes list
*
* @return java.awt.List
*/
private List getList() {
if (list == null) {
list = new List();
list.setBounds(new Rectangle(67, 16, 425, 206));
list.addItemListener(new java.awt.ev
12、ent.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
if (play == null) {
play(str, list);
b=true;
} else {
play.close();
play(str, list);
}
}
});
}
return list;
}
/**
* This method initializes
13、 jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(15, 244, 128, 31));
jButton.setText("添加歌曲文件夾");
jButton.addActionListener(this);
}
return jButton;
}
14、 public void actionPerformed(ActionEvent e) {
JFileChooser jchoose = new JFileChooser();
jchoose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
FileNameExtensionFilter filter = new FileNameExtensionFilter("MP3",
"MP3");
jchoose.setMultiSelectionEnabled(true);// 選擇文件夾
jcho
15、ose.setFileFilter(filter);
jchoose.setMultiSelectionEnabled(true);
jchoose.showOpenDialog(this);
if (jchoose.getSelectedFile() != null) {
if (list != null)
list.clear();
File file = jchoose.getSelectedFile();
str = jchoose.getCurrentDirectory()
+ jchoose.getSelected
16、File().getName() + "\\";
File f[] = file.listFiles();
for (int i = 0; i < f.length; i++) {
if (f[i].getName().endsWith(".mp3"))
list.add(f[i].getName());
}
}
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton
17、 getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(155, 246, 115, 30));
jButton1.setText("隨機(jī)播放");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
18、 // Thread Haha=new RadamPlay(list);
// Haha.start();
}
});
}
return jButton1;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButto
19、n2.setBounds(new Rectangle(280, 246, 81, 30));
jButton2.setText("暫停");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(play!=null)
{
play.stop();
}
}
});
20、
}
return jButton2;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if (jButton3 == null) {
jButton3 = new JButton();
jButton3.setBounds(new Rectangle(380, 247, 89, 29));
jButton3.setText("播放");
21、
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(play!=null)
{
play.start();
}
else
{
play(str, list);
}
}
});
}
return jButton3;
}
public static void main(String args[])
{
new Hello();
}
} // @jve:decl-index=0:visual-constraint="47,10"