gpt4 book ai didi

java - 无法让 JApplet 播放音乐 mp3 文件

转载 作者:行者123 更新时间:2023-12-01 14:35:25 25 4
gpt4 key购买 nike

我正在尝试让音乐在 JApplet 的后台播放。

小程序本身工作正常,但我听不到任何音乐。

我想知道这是否与文件是 mp3 有关。

    //AnimationDemo1.java

import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.applet.*;


public class Developers extends JApplet
{
private AudioClip backgroundmusic;



public void init()
{
URL urlformusic = getClass().getResource("audio/song1.mp3");
backgroundmusic = Applet.newAudioClip(urlformusic);
backgroundmusic.loop();
add(new DevelopersPanel());
}
public void start() {
backgroundmusic.loop();
}
public void stop(){
backgroundmusic.stop();
}
public void destroy() {
backgroundmusic.stop();
}
}// end of class of extended JApplet

class DevelopersPanel extends JPanel
{


private int numImages = 3;
private ImageIcon[] loop = new ImageIcon[numImages];
private String[] description = new String[3];
private int currentImage = 0;


public DevelopersPanel()
{
description[0] = "Charlie Brown works at Charleston Restraunt" +
"as a Shift Leader, Server, and Classroom Trainer.";
description[1] = "Snoopy, well he just does his own thing.";
description[2] = "Lucy helped keep everyone working on the project sane.";
for(int x = 0;x<loop.length;x++)
{
URL url = this.getClass().getResource("image/pic" + x + ".jpg");
loop[x] = new ImageIcon(url);
}

} //end of constructor

public void paintComponent(Graphics g)
{
super.paintComponent(g);
Dimension d = getSize();

g.drawImage(loop[currentImage].getImage(), 10, 10,d.width/2, d.height/2, this);
g.drawString(description[currentImage],d.width-d.width+20, d.height-20);
currentImage = (currentImage + 1) % numImages;

try{
Thread.sleep(3000);
}
catch(InterruptedException e){
}
repaint();

}

} //end of extended JPanel class

任何帮助将不胜感激。

我还是java新手,请保持简单。

最佳答案

I was wondering if it had to due with the file being mp3.

是的,确实如此。 Java 支持的标准格式数量非常有限。

要播放 MP3,我通常会使用 Java Sound 并将 MP3 服务提供者接口(interface)添加到运行时类路径中。请参阅Java Sound info. page了解更多详情。

关于java - 无法让 JApplet 播放音乐 mp3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16535937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com