導航:首頁 > 編程語言 > javaswing音樂播放器

javaswing音樂播放器

發布時間:2021-02-25 23:10:36

① 誰能幫忙用java做一個播放器

MML是什麼?
我做過一個MP3的..給你看看.

package music;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.media.bean.playerbean.*; //這個包要用到JMF

public class MP3 extends JFrame implements Runnable {
public JLabel shijian;

private JSlider sldDiameter;

public MediaPlayer soumd1;

public JButton playSound, loopSound, stopsound;

public JButton tjian, shanc, baocun, qu;

public JPanel jp1, jp2, jp3, jp4;

public JList jl;

int zongmiao=0;

public Vector vector, mingcheng;

boolean fo = false, geshi = false;

JLabel jl1, jl2, sj1, sj2;

JTextField jt1, jt2;

JButton queding, xiugai;

int zong = 0;

int a = 0, b = 0, you = 1,mm=0;

int fenzhong, miaozhong;

public MP3() {
super("java簡單音樂播放器");
soumd1 = new MediaPlayer();
Container c = getContentPane();
c.setLayout(new FlowLayout());
mingcheng = new Vector();
jp1 = new JPanel();
shijian = new JLabel();
jp1.add(shijian);
c.add(jp1);
playSound = new JButton("開始播放");
loopSound = new JButton("循環播放");
stopsound = new JButton("停止播放");
jp2 = new JPanel();
jp2.add(playSound);
jp2.add(loopSound);
jp2.add(stopsound);
c.add(jp2);
jp4 = new JPanel();
sj1 = new JLabel();
sj2 = new JLabel();
sldDiameter = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 0);
sldDiameter.setMajorTickSpacing(1);
sldDiameter.setPaintTicks(true);
jp4.add(sj1);
jp4.add(sldDiameter);
jp4.add(sj2);
c.add(jp4);
vector = new Vector();
jl = new JList(mingcheng);
jl.setVisibleRowCount(5);
jl.setFixedCellHeight(40);
jl.setFixedCellWidth(265);
jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
c.add(new JScrollPane(jl));
tjian = new JButton("添加");
shanc = new JButton("刪除");
qu = new JButton("讀取");
baocun = new JButton("保存");
jp3 = new JPanel();
jp3.add(tjian);
jp3.add(shanc);
jp3.add(baocun);
jp3.add(qu);
c.add(jp3);

try {
String s = "c:\\music.txt";
ObjectInputStream input = new ObjectInputStream(
new FileInputStream(s));
lujin a1 = (lujin) input.readObject();
mingcheng = a1.b;
vector = a1.a;
jl.setListData(mingcheng);
} catch (Exception e) {
System.out.println("c盤沒有保存音樂文件");
//e.printStackTrace();
}

baocun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser(); // 實例化文件選擇器
fileChooser
.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 設置文件選擇模式,此處為文件和目錄均可
if (fileChooser.showSaveDialog(MP3.this) == JFileChooser.APPROVE_OPTION) { // 彈出文件選擇器,並判斷是否點擊了打開按鈕
String fileName = fileChooser.getSelectedFile()
.getAbsolutePath(); // 得到選擇文件或目錄的絕對路徑
mmm(vector, mingcheng, fileName);
}
}
});
qu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser(); // 實例化文件選擇器
fileChooser
.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 設置文件選擇模式,此處為文件和目錄均可
if (fileChooser.showOpenDialog(MP3.this) == JFileChooser.APPROVE_OPTION) { // 彈出文件選擇器,並判斷是否點擊了打開按鈕
String fileName = fileChooser.getSelectedFile()
.getAbsolutePath(); // 得到選擇文件或目錄的絕對路徑
try {
ObjectInputStream input = new ObjectInputStream(
new FileInputStream(fileName));
lujin a1 = (lujin) input.readObject();
mingcheng = a1.b;
vector = a1.a;
jl.setListData(mingcheng);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
playSound.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jl.getSelectedIndex() >= 0) {
String yy = (String) vector.get(jl.getSelectedIndex());
File ff = new File(yy);
if (ff.exists()) {
if (yy.matches("[\\S\\s]*.mp3")|| yy.matches("[\\S\\s]*.MP3")) {
if (soumd1 != null) {
a = 0;
b = 0;
you = 0;
soumd1.stop();
}
soumd1.setMediaLocation("file:/" + yy);
fo = true;
soumd1.start();
geshi=true;
try {
Thread.sleep(500);
} catch (InterruptedException eee) {
}
zongmiao=(int)soumd1.getDuration().getSeconds();
if(zongmiao>10000)
{
try {
Thread.sleep(500);
} catch (InterruptedException ew) {
}
zongmiao=(int)soumd1.getDuration().getSeconds();
}
zongmiao=(int)soumd1.getDuration().getSeconds();
String aa=fen(zongmiao);
sj2.setText(aa);
} else
JOptionPane.showMessageDialog(null,
"播放文件格式的有錯,無法播放 建議刪除");
} else
JOptionPane.showMessageDialog(null,
"此歌曲文件已經不存在,建議刪除");
}
else
JOptionPane.showMessageDialog(null, "請選擇音樂文件");
}
});
loopSound.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jl.getSelectedIndex() >= 0) {
String yy = (String) vector.get(jl.getSelectedIndex());
File ff = new File(yy);
if (ff.exists()) {
if (yy.matches("[\\S\\s]*.mp3")|| yy.matches("[\\S\\s]*.MP3")) {
if (soumd1 != null) {
a = 0;
b = 0;
you = 0;
soumd1.stop();
}
soumd1.setMediaLocation("file:/" + yy);
fo = true;
soumd1.start();
geshi=true;
try {
Thread.sleep(500);
} catch (InterruptedException we) {
}
zongmiao=(int)soumd1.getDuration().getSeconds();
if(zongmiao>10000)
{
try {
Thread.sleep(500);
} catch (InterruptedException we) {
}
zongmiao=(int)soumd1.getDuration().getSeconds();
}
zongmiao=(int)soumd1.getDuration().getSeconds();
String aa=fen(zongmiao);
sj2.setText(aa);
} else
JOptionPane.showMessageDialog(null,
"播放文件格式的有錯,無法播放 建議刪除");
} else
JOptionPane.showMessageDialog(null,
"此歌曲文件已經不存在,建議刪除");

}
else
JOptionPane.showMessageDialog(null, "請選擇音樂文件");
}
});
stopsound.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = 0;
b = 0;
you = 0;
mm=0;
geshi=false;
fo = false;
sldDiameter.setMaximum(100);
sldDiameter.setValue(0);
sj1.setText(null);
sj2.setText(null);
if (jl.getSelectedIndex() >= 0)
soumd1.stop();

}
});
tjian.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser(); // 實例化文件選擇器
fileChooser
.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 設置文件選擇模式,此處為文件和目錄均可
fileChooser.setCurrentDirectory(new File(".")); // 設置文件選擇器當前目錄
fileChooser
.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File file) { // 可接受的文件類型
String name = file.getName().toLowerCase();
return name.endsWith(".mp3")
|| file.isDirectory();
}
public String getDescription() { // 文件描述
return "音樂文件(*.mp3)";
}
});
if (fileChooser.showOpenDialog(MP3.this) == JFileChooser.APPROVE_OPTION) { // 彈出文件選擇器,並判斷是否點擊了打開按鈕
String fileName = fileChooser.getSelectedFile()
.getAbsolutePath(); // 得到選擇文件或目錄的絕對路徑
vector.add(fileName);
StringBuffer buffer = xu(fileName);
mingcheng.add(buffer);
}
}
});
shanc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (jl.getSelectedIndex() >= 0) {
mingcheng.removeElementAt(jl.getSelectedIndex());
vector.removeElementAt(jl.getSelectedIndex());
jl.setListData(mingcheng);
}
}
});
jl.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
if (event.getClickCount() == 2) {
if (jl.getSelectedIndex() >= 0) {
String yy = (String) vector.get(jl.getSelectedIndex());
File ff = new File(yy);
if (ff.exists()) {
if (yy.matches("[\\S\\s]*.mp3")|| yy.matches("[\\S\\s]*.MP3")) {
if (soumd1 != null) {
a = 0;
b = 0;
you = 0;
soumd1.stop();
}
soumd1.setMediaLocation("file:/" + yy);
fo = true;
soumd1.start();
geshi=true;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
zongmiao=(int)soumd1.getDuration().getSeconds();
if(zongmiao>10000)
{
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
zongmiao=(int)soumd1.getDuration().getSeconds();
}
String aa=fen(zongmiao);
sj2.setText(aa);
} else
JOptionPane.showMessageDialog(null,
"播放文件格式的有錯,無法播放 建議刪除");
} else
JOptionPane.showMessageDialog(null,
"此歌曲文件已經不存在,建議刪除");

}
}
if (event.isMetaDown()) {
if (jl.getSelectedIndex() >= 0) {
int a = jl.getSelectedIndex();
xiugai x = new xiugai();
jt1.setText(mingcheng.get(jl.getSelectedIndex()) + "");
jt2.setText(vector.get(jl.getSelectedIndex()) + "");
}
}
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
String s = "c:\\music.txt";
mmm(vector, mingcheng, s);
System.exit(0);
}
});
setSize(300, 400);
setVisible(true);
}

public void run() {
while (true) {
Date now = new Date();
shijian.setText("當前時間: " + now.toString());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
if(geshi){
you=(int)soumd1.getMediaTime().getSeconds();
mm =you%60;
if(you!=0)
if(you%60==0)
{
b++;
}
sj1.setText(b+":"+mm);
sldDiameter.setMaximum(zongmiao);
if(you!=zongmiao)
sldDiameter.setValue(you);
else {
sldDiameter.setValue(0);
mm=0;
b=0;
}
}
}
}

public StringBuffer xu(String fileName) {
String mc = "";
for (int i = fileName.length(); i >= 1; i--) {
if (fileName.charAt(i - 1) == '\\')
break;
mc += fileName.charAt(i - 1);
}
StringBuffer buffer = new StringBuffer(mc);
StringBuffer mm = buffer.reverse();
return mm;
}

public void mmm(Vector vector, Vector mingcheng, String lujin) {
lujin a = new lujin(vector, mingcheng);
try {
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(lujin));
output.writeObject(a);
output.flush();
output.close();
} catch (Exception e) {

}
}
public String fen(int yy) {
fenzhong = (int) yy / 60;
miaozhong = (int) yy % 60;
String sss = fenzhong + ":" + miaozhong;
return sss;
}

public static void main(String agrs[]) {
MP3 s = new MP3();
Thread t1 = new Thread(s);
t1.start();
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

class xiugai extends JFrame {
public xiugai() {
jl1 = new JLabel("文件名");
jt1 = new JTextField(20);
jl2 = new JLabel("文件路徑");
jt2 = new JTextField(20);
xiugai = new JButton("修改");
Container c = getContentPane();
c.setLayout(new GridLayout(3, 1));
JPanel j1 = new JPanel();
JPanel j2 = new JPanel();
JPanel j3 = new JPanel();
j1.add(jl1);
j1.add(jt1);
j2.add(jl2);
j2.add(jt2);
j3.add(xiugai);
c.add(j1);
c.add(j2);
c.add(j3);
xiugai.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
mingcheng.setElementAt(jt1.getText(), jl.getSelectedIndex());
vector.setElementAt(jt2.getText(), jl.getSelectedIndex());
jl.setListData(mingcheng);
dispose();
}
});
setSize(300, 120);
setVisible(true);
}
}
}

class lujin implements Serializable {

Vector a = new Vector();

Vector b = new Vector();

public lujin(Vector vector, Vector mingcheng) {
a = vector;
b = mingcheng;
}

}

② 求一個JAVA音樂播放器的源代碼

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import javax.media.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MediaPlayer extends JFrame implements ActionListener,
ItemListener, ControllerListener {
String title;

Player player;
boolean first = true, loop = false;
Component vc, cc;
String currentDirectory=null;
// 構造函數,其中包括了設置響應窗口事件的監聽器。
MediaPlayer(String title) {
super(title);
/* 關閉按鈕的實現。。 */
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}

public void windowClosed(WindowEvent e) {
if (player != null)
player.close();
System.exit(0);
}

});
// 調用程序菜單欄的方法成員完成菜單的布置
setupMenu();
setSize(400, 400);
setVisible(true);
}

// 本方法用以設置程序菜單欄
public void setupMenu() {
// 設置一個菜單
Menu f = new Menu("文件");
// 往設置的菜單添加菜單項
MenuItem mi = new MenuItem("打開");
f.add(mi);
mi.addActionListener(this);
f.addSeparator();
CheckboxMenuItem cbmi = new CheckboxMenuItem("循環", false);
cbmi.addActionListener(this);
f.add(cbmi);
f.addSeparator();
MenuItem ee = new MenuItem("退出");
ee.addActionListener(this);
f.add(ee);
f.addSeparator();

Menu l = new Menu("播放列表");
Menu c = new Menu("播放控制");
MenuItem move = new MenuItem("播放");
move.addActionListener(this);
c.add(move);
c.addSeparator();
MenuItem pause = new MenuItem("暫停");
pause.addActionListener(this);
c.add(pause);
c.addSeparator();
MenuItem stop = new MenuItem("停止");
stop.addActionListener(this);
c.add(stop);
c.addSeparator();
// 設置一個菜單欄
MenuBar mb = new MenuBar();
mb.add(f);
mb.add?;
mb.add(l);
// 將構造完成的菜單欄交給當前程序的窗口;
setMenuBar(mb);
}

// 動作時間響應成員;捕捉發送到本對象的各種事件;
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String cufile, selectfile, currentDirectory;
if (e.getActionCommand().equals("退出")) {
// 調用dispose以便執行windowClosed
dispose();
return;
}
// 此事表明擁護選擇了「播放」命令;
// 如果當前有一個文件可以播放則執行播放命令;
if (e.getActionCommand().equals("播放")) {
if (player != null) {
player.start();
}
return;
}
// 如果當前正在播放某一文件,則執行暫停;
if (e.getActionCommand().equals("暫停")) {
if (player != null) {
player.stop();
}
return;
}
// 停止命令的響應;
if (e.getActionCommand().equals("停止")) {
if (player != null) {
player.stop();
player.setMediaTime(new Time(0));
}
return;
}
// 用戶選擇要播放的媒體文件
if (e.getActionCommand().equals("打開")) {
FileDialog fd = new FileDialog(this, "打開媒體文件", FileDialog.LOAD);
// fd.setDirectory(currentDirectory);

2008-2-6 02:46 回復

肆方茉莉
62位粉絲
6樓

fd.setVisible(true);
// 如果用戶放棄選擇文件,則返回
if (fd.getFile() == null) {
return;
}
// 保存了所選文件的名稱及其路徑名稱已被稍後使用
// 同時設置當前文件夾路徑
selectfile = fd.getFile();
currentDirectory = fd.getDirectory();
cufile = currentDirectory + selectfile;
// 將用戶選擇的文件作為一個菜單項加入播放列表,該菜單項名為該文件名;
// 被點擊後給出的命令串是該文件的全路徑名
MenuItem mi = new MenuItem(selectfile);
mi.setActionCommand(cufile);
MenuBar mb = getMenuBar();
Menu m = mb.getMenu(2);
mi.addActionListener(this);
m.add(mi);
} else {
// 程序邏輯運行到次表示用戶選擇了一個「播放列表」中的媒體文件
// 此時可以通過如下動作獲得該文件的全路徑名
cufile = e.getActionCommand();
selectfile = cufile;
}
// 如果存在一個播放器,則先將其關閉,稍後再重新創建
// 創建播放器時需要捕捉一些異常
if (player != null) {
player.close();
}
try {
player = Manager.createPlayer(new MediaLocator("file:" + cufile));
} catch (Exception e2) {
System.out.println(e2);
return;
}/*
* catch(NoPlayerException e2){ System.out.println("不能找到播放器");
* return ; }
*/
if (player == null) {
System.out.println("無法創建播放器");
return;
}
first = false;
setTitle(selectfile);
// 設置處理播放控制器實際的對象;
/**/
player.addControllerListener(this);
player.prefetch();
}

// 菜單狀態改變事件的響應函數;
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub

}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MediaPlayer("播放器");
}

// 調用繪圖函數進行界面的繪制 // public void update() {
// }
// 繪圖函數成員 //public void paint(Graphics g) {
// }
public void controllerUpdate(ControllerEvent e) {
// TODO Auto-generated method stub
Container tainer = getContentPane();
// 調用player.close()時ControllerClosedEvent事件出現
// 如果存在視覺部件,則該部件應該拆除(為了一致起見,我們對控制面版部件也執行同樣的操作,下一次需要時再構造)
if (e instanceof ControllerClosedEvent) {
if (vc != null) {
remove(vc);
vc = null;
}
if (cc != null) {
remove(cc);
cc = null;
}
}

// 播放結束時,將播放指針置於文件之首,如果設定了循環播放,則再次啟動播放器;
if (e instanceof EndOfMediaEvent) {
player.setMediaTime(new Time(0));
if (loop) {
player.start();
}
return;
}

// PrefetchCompletEvent事件發生後調用start,正式啟動播放
if (e instanceof PrefetchCompleteEvent) {
player.start();
return;
}

// 本事件表示由於播放的資源已經確定;此時要將媒體的圖形conmopnent
// 如果有顯示出來,同時將播放器player的控制顯示到窗口裡;
if (e instanceof RealizeCompleteEvent) {
// 如果媒體中有圖像,將對應圖像component載入窗體;
vc = player.getVisualComponent();
if (vc != null)
tainer.add(vc, BorderLayout.CENTER);
// 將對應控制器component載入窗體;
cc = player.getControlPanelComponent();
cc.setBackground(Color.blue);
if (cc != null)
tainer.add(cc, BorderLayout.SOUTH);
// 有一些特殊媒體在播放時提供另外的控制手段,將控制器一並加入窗口;
/*
* gc=player.getGainControl(); gcc=gc.getControlComponent();
* if(gcc!=null) tainer.add(gcc,BorderLayout.NORTH);
*/
// 根據媒體文件中是否有圖像,設定相應的窗口大小
if (vc != null) {
pack();
return;
} else {
setSize(300, 75);
setVisible(true);
return;
}
}

} }

③ 怎樣通過java打開音樂播放器

java中打開音樂播放器的方式是使用audioclip類來播放音樂,實例如下:

importjava.applet.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.net.*;

importjavax.swing.*;
importjava.io.File;

{
booleanlooping=false;
Filefile1=newFile("music\明天會更好.wav");
AudioClipsound1;
AudioClipchosenClip;

JButtonplayButton=newJButton("播放");
JButtonloopButton=newJButton("循環播放");
JButtonstopButton=newJButton("停止");
JLabelstatus=newJLabel("選擇播放文件");
JPanelcontrolPanel=newJPanel();
Containercontainer=getContentPane();

publicAudioPlayDemo(){
try{
sound1=Applet.newAudioClip(file1.toURL());
chosenClip=sound1;
}catch(OutOfMemoryErrore){
System.out.println("內存溢出");
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}
playButton.addActionListener(this);
loopButton.addActionListener(this);
stopButton.addActionListener(this);
stopButton.setEnabled(false);

controlPanel.add(playButton);
controlPanel.add(loopButton);
controlPanel.add(stopButton);

container.add(controlPanel,BorderLayout.CENTER);
container.add(status,BorderLayout.SOUTH);

setSize(300,130);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//關閉窗口時退出程序
}

publicvoidactionPerformed(ActionEventevent){
if(chosenClip==null){
status.setText("聲音未載入");
return;
}
Objectsource=event.getSource();//獲取用戶洗滌激活的按鈕

if(source==playButton){
stopButton.setEnabled(true);
loopButton.setEnabled(true);
chosenClip.play();
status.setText("正在播放");
}

if(source==loopButton){
looping=true;
chosenClip.loop();
loopButton.setEnabled(false);
stopButton.setEnabled(true);
status.setText("正在循環播放");
}
if(source==stopButton){
if(looping){
looping=false;
chosenClip.stop();
loopButton.setEnabled(true);
}else{
chosenClip.stop();
}
stopButton.setEnabled(false);
status.setText("停止播放");
}
}
publicstaticvoidmain(Strings[]){
newAudioPlayDemo();
}
}

只能播放wav格式的歌曲

④ JAVA的SWING中哪個控制項可以播放音樂

any class' methods of Swing cannot play sounds.

but you can use:Applet,JMF and Java Sound API(javax.sound.sampled.* and javax.sound.midi.*)

Swing is only GUI components.

⑤ java製作音樂播放器要求實現換膚歌詞顯示!誰有製作視頻急求

Swing窗口可以通過 更改 Look and Feel 來更好主題, 實現皮膚的更換.

LRC文件,可以通過IO流讀取到內存專,然後進行解析. 在聲音屬播放到指定的時間點,顯示歌詞

⑥ java 音樂播放器

//哈哈昨晚上剛做了一個基本功能是有美化和完善就靠你了,代碼如下:
//順便說一下你電腦里需要有javax.media包才行,沒有的話和我說。
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.File;
importjavax.media.Manager;
importjavax.media.MediaLocator;
importjavax.media.Player;
importjavax.swing.AbstractAction;
importjavax.swing.JButton;
importjavax.swing.JFileChooser;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
publicclassMusicDemo{
publicstaticvoidmain(Stringargs[])throwsInterruptedException{
MusicPlayermp=newMusicPlayer("音樂播放器");
}
}
{
JButtonbtnOK=newJButton("選歌");
JLabellblCaption=newJLabel("Hello!China!");
JButtonbtnCancel=newJButton("播放");
Playerplay=null;
Stringpath="";
MusicPlayer(Stringtitile){
super(titile);
doublely=Toolkit.getDefaultToolkit().getScreenSize().getHeight();
doublelx=Toolkit.getDefaultToolkit().getScreenSize().getWidth();
setLocation((int)lx/3,(int)ly/3);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,200);
btnOK.setBounds(0,0,80,30);
this.getContentPane().add(btnOK);
lblCaption.setBounds(5,40,120,30);
this.getContentPane().add(lblCaption);
btnCancel.setBounds(120,0,80,30);
this.getContentPane().add(btnCancel);
btnOK.addActionListener(newAbstractAction(){
publicvoidactionPerformed(ActionEvente){
lblCaption.setText("YouClickOK!");
path=open();
}
}
);
btnCancel.addActionListener(newAbstractAction(){
publicvoidactionPerformed(ActionEvente){
lblCaption.setText("YouClickCancel!");
try{
Filefile=newFile(path);
if(play==null){
if(file.exists()){

MediaLocatorlocator=newMediaLocator("file:"+file.getAbsolutePath());
play=Manager.createRealizedPlayer(locator);
play.prefetch();
add(play.getControlPanelComponent(),"South");
play.start();
}
}
}catch(Exceptione1){
e1.printStackTrace();
}

}
});
}
Stringopen(){
StringfilePath="";
JFileChooserfileChooser=newJFileChooser();
fileChooser.setDialogTitle("選擇歌...");
//fileChooser.setFileSelectionMo(JFileChooser.DIRECTORIES_ONLY);
intreturnVal=fileChooser.showOpenDialog(fileChooser);
if(returnVal==JFileChooser.APPROVE_OPTION){
filePath=fileChooser.getSelectedFile().getAbsolutePath();
}
returnfilePath;
}
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
}
}

⑦ java中Swing怎麼添加音樂

JAVA應用程序添加背景音樂示例:

1.先定義一個 URL 對象,並賦NULL值;

URL musicURL = null;

2.然後為定義的 URL 賦值,即得到要播放的URL地址,

musicURL = new URL( "音樂地址");//得到要播放音樂的url

3.創建一個播放音頻的實例,並得到這個實例

AudioClip ac = Applet.newAudioClip(musicURL); //得到一個播放音頻的實例

4.進行播放控制

ac.play(); //播放一編

ac.loop(); //循環播放

ac.stop();//停止

播放本地音樂示例:

private void playMusic(){
try {
javax.media.Player player;
if(null==player){
File musicFile=new File("./music.mp3");//得到一個MP3文件
if(musicFile.exists()){
MediaLocator locator=new MediaLocator("file:"+musicFile.getAbsolutePath());
player = Manager.createRealizedPlayer(locator);
player.prefetch();//預讀文件
}else{
System.err.println(pro.getMusicFile()+" 找不到");
}
}
//centerpanel.add(player.getControlPanelComponent()!=null?player.getControlPanelComponent():null);//顯示播放工具
player.start();//播放
} catch (CannotRealizeException ex) {
ex.printStackTrace();
} catch (NoPlayerException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}

⑧ 單擊一個按鈕時播放音樂怎麼用java swing來實現的

AudioClip sound=Applet.newAudioClip(url)

sound.play();

⑨ java編音樂播放器

package bofan;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.File.*;
import java.io.*;
import javax.media.ControllerClosedEvent;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
import java.beans.*;

public class yinyue{
private static String path = "a.mp3";

public static void main(String[] args) {
File f1 = new File(path);
try {
Player player = Manager.createRealizedPlayer(f1.toURI().toURL());
System.out.println("獲得文件播放時間: "
+ player.getDuration().getSeconds());
player.prefetch();
player.setMediaTime(new Time(10.0));//從莫個時間段開始播放
player.start();
}catch(Exception ex){}
}
}

⑩ JAVA做音樂播放器簡單嗎

思路很簡單,就是用第三方的java mp3解碼器完成文件播放功能,然後用swing組件包裝一下就行版。剛好業界權有一個JMF java media framework,是大名鼎鼎的sun開發的。詳看這里的官方網頁:http://java.sun.com/procts/java-media/jmf/。網上也有很多JMF教程
我曾經做過一個簡單的,支持文件列表。這是一個很好的編程訓練。

閱讀全文

與javaswing音樂播放器相關的資料

熱點內容
win10電腦圖標點不開怎麼辦 瀏覽:704
蘋果7的網路信號不好怎麼辦 瀏覽:223
男同激情電影 瀏覽:62
國外的電影去哪裡下載 瀏覽:991
送快遞電影 瀏覽:301
聯相電腦win7換win10 瀏覽:846
玄幻肉 瀏覽:991
pwd4.ocm 瀏覽:915
如何在linux安裝tomcat 瀏覽:974
哪個網校的編程最好 瀏覽:717
塞巴斯蒂安1976南瓜影院 瀏覽:662
百分點大數據操作系統 瀏覽:292
系統開店經營流小說 瀏覽:789
數據差異很大用什麼數代表 瀏覽:54
韓國有個說女僕的電影 瀏覽:41
古裝有色電影 瀏覽:441
90版本什麼職業吃異界 瀏覽:151
政府招聘大數據人才 瀏覽:565
電影天堂禁止訪問 瀏覽:624
燕秀工具箱2017 瀏覽:883

友情鏈接