简体   繁体   中英

How do you add music to a JFrame?

import java.awt.*;
import javax.swing.*;

public class TestFrame1 {
    public static void main(String[] args) {

        JFrame frame = new JFrame("Test Frame 1");
        frame.setSize(200, 100);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

I need some help adding music to jframe. i been looking only for a good tutorial, and none of them seem to work.

im using netbeans. here is my current code. i just want to add music to the frame no stop button for now. Thank you.

Try:

public static void playSong(URL media) {
    Player mediaPlayer = Manager.createRealizedPlayer(media);
    mediaPlayer.start()
}

So you should just be able to call that method and pass in the URL to the media, and then it should play (Note: I have not tested this code).

The imports you need are:

import javax.media.Player;
import java.net.URL;

I just remembered, you need to add the JMF .jar to your project. The JMF (Java Media Framework) has tools for playing music and (I think) video, among other things.

Here is a pretty extensive tutorial from IBM: http://www.ibm.com/developerworks/java/tutorials/j-jmf/

Towards the bottom, it has instructions on installing JMF, and then on the next page it shows you how to make basic audio.

Some more advice:

1) You need to add the mp3 plug in to play mp3s from the JMF. After adding the plug in .jar file to your project, this is the code you haft to add (I'm doing this from memory, so it may be wrong):

    Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
    Format input2 = new AudioFormat(AudioFormat.MPEG);
    Format output = new AudioFormat(AudioFormat.LINEAR);
    PlugInManager.addPlugIn(
        "com.sun.media.codec.audio.mp3.JavaDecoder",
        new Format[]{input1, input2},
        new Format[]{output},
        PlugInManager.CODEC
    );

2) The last time I used it, the JMF download link was broken on the oracle website (it linked to the wrong page), so you may have to search around for the link on google.

Have a look at Accessing Audio System Resources . Here are the available classes

Class              Format
---------------------------------------------
AudioSystem        WAV
Manager*           MP3     
MidiSystem         Midi

javax.media.Manager requires Java Media Framework

The easiest options are AudioSystem or MidiSystem a they require no additional JAR files. Here is an example from the tag link

public class LoopSound {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
                          "http://pscode.org/media/leftright.wav");
        Clip clip = AudioSystem.getClip();
        // getAudioInputStream() also accepts a File or InputStream
        AudioInputStream ais = AudioSystem.getAudioInputStream( url );
        clip.open(ais);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // A GUI element to prevent the Clip's daemon Thread 
                // from terminating at the end of the main()
                JOptionPane.showMessageDialog(null, "Close to exit!");
            }
        });
    }
}

To integrate the the audio with the JFrame, simply invoke Clip#loop when the application is started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM