Applet 播放音频问题 Applet.newAudioClip(file.toURL());

package guitest;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class MusicPalyer extends JFrame{
File file;
TextField t=new TextField(20);
public MusicPalyer() {
this.add(b1());
this.add(t);
this.add(b2());
this.setSize(300, 100);
this.setVisible(true);
this.setLayout(new FlowLayout());
}

private Button b1(){
    Button b=new Button("choose");
    b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser jf = new JFileChooser();
            jf.showOpenDialog(null);
            file = jf.getSelectedFile();
            t.setText(file.getAbsolutePath());
        }
    });
    return b;
}
private Button b2(){
    Button  b1=new Button("paly");
    b1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(file!=null){
                AudioClip au = null;
                try {
                    if(au!=null){au.stop();}
                    au = Applet.newAudioClip(file.toURL());
                    au.play();

                } catch (MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            }   

        }
    });
    return b1;
}

public static void main(String[] args) {
    new MusicPalyer();
}

}

如上,wav音频文件已选择好,为什么播放不了啊

1个回答

播放后面加Thread.Sleep(10000);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐