简体   繁体   中英

Can't get JApplet to play music mp3 file

I am trying to get music to play in the background on a 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.

    //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.

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

Yes, it is. Java supports a very limited number of formats as standard.

To play MP3 I would typically use Java Sound and add an MP3 Service Provider Interface to the run-time class-path. See the Java Sound info. page for more details.

There is a small and very easy of use java library. It provides a sound player supporting mp3, MIDI, wav ... It has support to play single files, folders and m3u lists. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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