|
| ||||||||||
| Tags: java, jframe, music, programming |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Insert Music in JFrame From Java
|
|
#2
| ||||
| ||||
| Re: Insert Music in JFrame From Java
Hello, I am having a code below which if you use for your inserting music in your JFrame then you will able to achive it. So, you need to declare on JFrame and add the below code in that JFrame for getting your program. So, just do that and get your problem solved. Code: import java.awt.*;
import java.awt.event.*;
public class JavaAudio extends java.applet.Applet implements ActionListener
{
java.applet.AudioClip sound;
public void init() {
sound = getAudioClip( getClass().getResource(getParameter("sound")) );
Button b1 = new Button("Audio");
b1.addActionListener( this );
add ( b1 );
}
public void actionPerformed( ActionEvent ae ) {
if ( sound != null )
sound.play();
}
} |
|
#3
| |||
| |||
| Re: Insert Music in JFrame From Java
Although there are many situations in which you could use either a Clip or a SourceDataLine, the following criteria help identify which kind of line is better suited for a particular situation:
|
|
#4
| ||||
| ||||
| Re: Insert Music in JFrame From Java
Hi, I think you need to simply make use of the action listener if you wan to use the music file in your JFrame. You can use the line of code below in your program and get your problem solved: Code: AudioClip aClip = Applet.newAudioClip("play.wav");
aClip.play(); |
|
#5
| ||||
| ||||
| Re: Insert Music in JFrame From Java
Hey, I have got the code below which is simple and I think it would not need any kind of explanation: Code: mport java.applet.*;
import java.awt.*;
/*
<applet code="TestAudio" width=300 height=50>
<param name="audioTest" value="test.au">
</applet>
*/
public classjavaSound extends Applet
{
AudioClip audioTest;
public void init()
{
audioTest = getAudioClip(getDocumentBase(), getParameter("audioTest"));
audioTest.play();
}
public void paint(Graphics g)
{
g.drawString("Played" + getParameter("audioTest"), 26, 26);
}
} |
|
#6
| ||||
| ||||
| Re: Insert Music in JFrame From Java
Hello, If you want to make use of the code which will provide you different audio clips then you can simply make use of the code below and get your problem solved: Code: import java.applet.*;
import java.awt.*;
import java.util.Random;
public class AudCheck extends Applet implements Runnable
{
AudioClip audioC[] = new AudioClip[8];
Thread thrd;
Random rand = new Random(System.currentTimeMillis());
public void init()
{
audioC[0] = getAudioClip(getDocumentBase(), "Hillbilly.au");
audioC[1] = getAudioClip(getDocumentBase(), "Cow.au");
audioC[2] = getAudioClip(getDocumentBase(), "Duck.au");
audioC[3] = getAudioClip(getDocumentBase(), "Goat.au");
audioC[4] = getAudioClip(getDocumentBase(), "Hen.au");
audioC[5] = getAudioClip(getDocumentBase(), "Horse.au");
audioC[6] = getAudioClip(getDocumentBase(), "Pig.au");
audioC[7] = getAudioClip(getDocumentBase(), "Rooster.au");
}
public void start()
{
if (thrd == null)
{
thrd = new Thread(this);
thrd.start();
}
}
public void stop()
{
if (thrd != null)
{
thrd.stop();
thrd = null;
}
}
public void run()
{
while (Thread.currentThread() == thrd)
{
audioC[0].loop();
while (true)
{
try
Thread.sleep(3000);
catch (InterruptedException e)
break;
audioC[(rand.nextInt() % 3) + 4].play();
}
}
}
public void paint(Graphics g)
{
Font fnt = new Font("TimesRoman", Font.PLAIN, 20);
FontMetrics fontmetrics = g.getFontMetrics(fnt);
String str = new String("Testing");
g.setFont(fnt);
g.drawString(str, (size().width - fontmetrics.stringWidth(str)) / 2,
((size().height - fontmetrics.getHeight()) / 2) + fontmetrics.getAscent());
}
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Insert Music in JFrame From Java" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| JAVA: Image and Video in JFrame | Zia 7 | Software Development | 4 | 30-08-2010 07:35 PM |
| Disabling the Close Button on a JFrame in Java | technika | Software Development | 5 | 24-02-2010 09:51 PM |
| What is the use of JFrame class? | Ram Bharose | Software Development | 5 | 17-02-2010 09:56 AM |
| Java program to center a JFrame on screen | KALANI84 | Software Development | 5 | 13-02-2010 04:48 PM |
| Insert a java skyblog | Kishan | Software Development | 2 | 25-12-2008 08:36 PM |