java 音频播放器_JAVA音频播放器问题
代碼如下,請高手幫忙解決importjava.applet.*;importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjava.net.URL;importjavax.swing.*;classMyJFrameextendsJFrameimple...
代碼如下,請高手幫忙解決
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;
import javax.swing.*;
class MyJFrame extends JFrame implements ActionListener
{
JButton bt1,bt2,bt3,bt4;
JPanel p1;
AudioClip ac;
MyJFrame()
{
this.setTitle("音樂播放器"); //標題
this.setSize(300,400);
p1=new JPanel();
p1.setLayout(new FlowLayout());
bt1=new JButton("打開");
bt1.addActionListener(this);
bt2=new JButton("播放");
bt2.addActionListener(this);
bt3=new JButton("循環(huán)");
bt3.addActionListener(this);
bt4=new JButton("停止"); //增加四按鈕
bt4.addActionListener(this);
p1.add(bt1);
p1.add(bt2);
p1.add(bt3);
p1.add(bt4);
this.add(p1);
this.setVisible(true);
this.addWindowListener(new WL()); //窗口監(jiān)聽
}
public void actionPerformed(ActionEvent e)
{
File fl,f2; //兩個文件對象
String fpath,fname; //分別得到文件所在文件夾和文件名
URL url=null;
Applet ap=new Applet(); //定義Applet對象,調用其getAudioClip函數(shù)得到音頻
if(e.getActionCommand()=="打開") //運用文件選擇器
{
JFileChooser jfc = new JFileChooser();
if(jfc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
{
fl=jfc.getSelectedFile(); //得到所選的文件
f2=fl.getParentFile(); //得到上級目錄文件
fpath=f2.getAbsolutePath(); //得到上級目錄地址
fname=fl.getName(); //得到文件名
try{
url=new URL("file://"+fpath); //把上級目錄地址轉化成URL對象
ac=ap.getAudioClip(url, fname); //通過Applet對象得到音頻
}catch(IOException ex){}
}
}
if(e.getActionCommand()=="播放")
{
ac.play();
}
if(e.getActionCommand()=="循環(huán)")
{
ac.loop();
}
if(e.getActionCommand()=="停止")
{
ac.stop();
}
}
class WL extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}
}
public class Music {
public static void main(String[] args) {
new MyJFrame();
}
}
就是打開的時候有錯誤,也播放不了
就是AudioClip 對象ac沒有得到音頻,請問如何解決呢?謝謝了!
展開
總結
以上是生活随笔為你收集整理的java 音频播放器_JAVA音频播放器问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hive报错Could not get
- 下一篇: 【Robo3T】为mongodb数据库中