Reputation: 17
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.
Upvotes: 0
Views: 1429
Reputation: 11
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
Upvotes: 0
Reputation: 168835
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.
Upvotes: 1