Results 1 to 6 of 6

Thread: Insert Music in JFrame From Java

  1. #1
    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.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,949

    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. #3
    Join Date
    May 2008
    Posts
    2,015

    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.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,010

    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. #5
    Join Date
    Oct 2005
    Posts
    2,394

    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. #6
    Join Date
    May 2008
    Posts
    2,302

    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());
    }
    }

Similar Threads

  1. JAVA: Image and Video in JFrame
    By Zia 7 in forum Software Development
    Replies: 4
    Last Post: 30-08-2010, 07:35 PM
  2. Disabling the Close Button on a JFrame in Java
    By technika in forum Software Development
    Replies: 5
    Last Post: 24-02-2010, 09:51 PM
  3. What is the use of JFrame class?
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 17-02-2010, 09:56 AM
  4. Java program to center a JFrame on screen
    By KALANI84 in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 04:48 PM
  5. Insert a java skyblog
    By Kishan in forum Software Development
    Replies: 2
    Last Post: 25-12-2008, 08:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •