简体   繁体   English

无法让JApplet播放音乐mp3文件

[英]Can't get JApplet to play music mp3 file

I am trying to get music to play in the background on a JApplet. 我正在尝试让音乐在JApplet的后台播放。

The applet itself works just fine but I dont hear any music. 小程序本身工作正常,但我听不到任何音乐。

I was wondering if it had to due with the file being mp3. 我想知道是否必须将文件设为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

Any help would be very much appreciated. 任何帮助将不胜感激。

I am still new to java please keep it simple. 我对Java还是陌生的,请保持简单。

I was wondering if it had to due with the file being mp3. 我想知道是否必须将文件设为mp3。

Yes, it is. 是的。 Java supports a very limited number of formats as standard. Java作为标准仅支持非常有限数量的格式。

To play MP3 I would typically use Java Sound and add an MP3 Service Provider Interface to the run-time class-path. 要播放MP3,我通常会使用Java Sound,并将MP3服务提供程序接口添加到运行时类路径。 See the Java Sound info. 请参阅Java声音信息。 page for more details. 页面以获取更多详细信息。

There is a small and very easy of use java library. 有一个很小且非常易于使用的java库。 It provides a sound player supporting mp3, MIDI, wav ... It has support to play single files, folders and m3u lists. 它提供了一个支持mp3,MIDI,wav的声音播放器。它支持播放单个文件,文件夹和m3u列表。 The player also has functionalities like loop and shuffle among other useful features. 播放器还具有诸如循环和随机播放之类的功能,以及其他有用的功能。 You can get all the source code and api documentation from: http://imr-lib.blogspot.com 您可以从以下网址获取所有源代码和api文档: http : //imr-lib.blogspot.com

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

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