Insert Music in JFrame From Java
Hello, I am learning java programming language and while learning it, I want to know How to insert the music in Java JFrame? I have tried to search about it, and while searching it I don't find out any kind of useful material, so if you are having any type of solution for getting this code or any details from which I can able to get the solution for it, then I will be thankful to you.
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();
}
}
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:
- Use a Clip when you have non-real-time sound data that can be preloaded into memory. For example, you might read a short sound file into a clip. If you want the sound to play back more than once, a Clip is more convenient than a SourceDataLine, especially if you want the playback to loop. If you need to start the playback at an arbitrary position in the sound, the Clip interface provides a method to do that easily.
- Use a SourceDataLine for streaming data, such as a long sound file that won't all fit in memory at once, or a sound whose data can't be known in advance of playback. As an example of the latter case, suppose you're monitoring sound input—that is, playing sound back as it's being captured. If you don't have a mixer that can send input audio right back out an output port, your application program will have to take the captured data and send it to an audio-output mixer. In this case, a SourceDataLine is more appropriate than a Clip.
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();
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);
}
}
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());
}
}