如何为应用程序和小程序播放 mp3 文件?
How do I play an mp3 file for both an application and applet?
我正在开发一个程序,当您在小程序和应用程序中单击按钮时都会播放声音,但在程序的应用程序部分我一直收到错误消息。
这是我得到的错误:sample.mp3(系统找不到指定的文件)
但我的项目中显然有它。
public class project extends JApplet {
public void init() {
add(new AppletOrApplicationMainComponent());
}
public static void main(String args[]) {
JFrame frame = new JFrame("");
frame.getContentPane().add(new AppletOrApplicationMainComponent());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
class AppletOrApplicationMainComponent extends JPanel {
public AppletOrApplicationMainComponent() {
super(new BorderLayout());
JButton button = new JButton("Play");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Play();
}
});
add(button, BorderLayout.SOUTH);
}
private void Play(){
try{
File file = new File("sample.mp3");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
Player player = new Player(bis);
player.play();
}catch(Exception e){
e.printStackTrace();
}
}
}
太激动了,差点忘了回答问题:
How do I play an mp3 file for both an application and applet?
首先,使用URL访问资源是兼容applet和application的。 URL 可以从 Class.getResource(String)
. For more details on using the method, see the embedded resource info. page.
获得
关于播放我们有 URL 的 MP3,请参阅 Java Sound info. page 的 MP3 解码支持部分。基本上它归结为将 MP3 SPI 添加到应用程序的 运行-时间 class-路径。
另请注意,该页面中提到的 Clip
便利性 class(并在示例中显示)不适用于大文件。从内存来看,它最多可以保存 1 秒的 CD 质量(立体声、16 位、44.1 KHz)声音。对于较大的文件,您可以直接使用 AudioInputStream
,读取字节并将它们反馈给播放它们的声音 API。或者使用 BigClip
,它将整个流字节缓存在内存中,很像 Clip
.
我正在开发一个程序,当您在小程序和应用程序中单击按钮时都会播放声音,但在程序的应用程序部分我一直收到错误消息。
这是我得到的错误:sample.mp3(系统找不到指定的文件) 但我的项目中显然有它。
public class project extends JApplet {
public void init() {
add(new AppletOrApplicationMainComponent());
}
public static void main(String args[]) {
JFrame frame = new JFrame("");
frame.getContentPane().add(new AppletOrApplicationMainComponent());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
class AppletOrApplicationMainComponent extends JPanel {
public AppletOrApplicationMainComponent() {
super(new BorderLayout());
JButton button = new JButton("Play");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Play();
}
});
add(button, BorderLayout.SOUTH);
}
private void Play(){
try{
File file = new File("sample.mp3");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
Player player = new Player(bis);
player.play();
}catch(Exception e){
e.printStackTrace();
}
}
}
太激动了,差点忘了回答问题:
How do I play an mp3 file for both an application and applet?
首先,使用URL访问资源是兼容applet和application的。 URL 可以从 Class.getResource(String)
. For more details on using the method, see the embedded resource info. page.
关于播放我们有 URL 的 MP3,请参阅 Java Sound info. page 的 MP3 解码支持部分。基本上它归结为将 MP3 SPI 添加到应用程序的 运行-时间 class-路径。
另请注意,该页面中提到的 Clip
便利性 class(并在示例中显示)不适用于大文件。从内存来看,它最多可以保存 1 秒的 CD 质量(立体声、16 位、44.1 KHz)声音。对于较大的文件,您可以直接使用 AudioInputStream
,读取字节并将它们反馈给播放它们的声音 API。或者使用 BigClip
,它将整个流字节缓存在内存中,很像 Clip
.