Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Insert Music in JFrame From Java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 05-03-2010
Member
 
Join Date: Nov 2009
Posts: 140
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.

Reply With Quote
  #2  
Old 05-03-2010
Member
 
Join Date: Apr 2008
Posts: 1,946
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();
    }
}
Reply With Quote
  #3  
Old 05-03-2010
Member
 
Join Date: May 2008
Posts: 2,004
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.
Reply With Quote
  #4  
Old 05-03-2010
Member
 
Join Date: Apr 2008
Posts: 1,999
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();
Reply With Quote
  #5  
Old 05-03-2010
Member
 
Join Date: Oct 2005
Posts: 2,388
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);
}

}
Reply With Quote
  #6  
Old 05-03-2010
Member
 
Join Date: May 2008
Posts: 2,289
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());
}
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 04:45 AM.