简体   繁体   English

如何为应用程序和小程序播放mp3文件?

[英]How do I play an mp3 file for both an application and applet?

I working on a program that both plays a sound when you click a button in an applet and in an application but I keep receiving an error while in the application portion of the program. 我正在开发一个程序,当您在applet和应用程序中单击一个按钮时,它们都可以发出声音,但是在该程序的应用程序部分我一直收到错误消息。

This is the error I get: sample.mp3 (The system cannot find the file specified) but I clearly have it in my project. 这是我得到的错误: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();
    }
}
}

In all that excitement, almost forgot to answer the question: 在所有激动中,几乎忘了回答这个问题:

How do I play an mp3 file for both an application and applet? 如何为应用程序和小程序播放mp3文件?

Firstly, using URL to access a resource is compatible with both applet and application. 首先,使用URL访问资源与applet和应用程序兼容。 The URL can be obtained from Class.getResource(String) . 可以从Class.getResource(String)获得URL。 For more details on using the method, see the embedded resource info. 有关使用该方法的更多详细信息,请参见嵌入式资源信息。 page . 页面

As to playing an MP3 for which we have an URL, see the MP3 decoding support section of the Java Sound info. 关于播放我们拥有URL的MP3,请参阅Java Sound信息的MP3解码支持部分 page . 页面 Basically it boils down to adding an MP3 SPI to the run-time class-path of the app. 基本上可以归结为将MP3 SPI添加到应用程序的运行时类路径。

Also note that the Clip convenience class mentioned in that page (and shown in the example) does not work with large files. 另请注意,该页面中提到的Clip便利类(如示例所示)不适用于大文件。 From memory, it will hold at most 1 second of CD quality (stereophonic, 16 bit, 44.1 KHz) sound. 从内存中,它将最多保留1秒钟的CD质量(立体声,16位,44.1 KHz)声音。 For larger files you might use an AudioInputStream directly, reading the bytes and feeding them back out to the sound API that plays them. 对于较大的文件,您可以直接使用AudioInputStream ,读取字节并将其反馈给播放它们的声音API。 Either that or use BigClip , which caches the entire stream bytes in memory, much like Clip . 要么使用BigClip ,要么将整个流字节缓存在内存中,就像Clip一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM