Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

import javax.swing.*;
import sun.audio.*;
import java.awt.event.*;
import java.io.*;

public class Sound {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(200,200);
        JButton button = new JButton("Clcik me");
        frame.add(button);
        button.addActionListener(new AL());
        frame.show(true);
    }
    public static class AL implements ActionListener{
        public final void actionPerformed(ActionEvent e){
            music();
        }
    }
    public static void music(){
        AudioPlayer MGP = AudioPlayer.player;
        AudioStream BGM;
        AudioData MD;
        ContinuousAudioDataStream loop = null;
        try{
            BGM = new AudioStream(new FileInputStream("backgroundmusic.wav"));
            MD = BGM.getData();
            loop = new ContinuousAudioDataStream(MD);
        }catch(IOException error){
            error.printStackTrace();
        }
        MGP.start(loop);
    }
}

When I run this code, I see following warnings. Why and how do I avoid that?

Sound.java:22: warning: AudioPlayer is internal proprietary API and may be removed in a future release
Sound.java:23: warning: AudioStream is internal proprietary API and may be removed in a future release
Sound.java:24: warning: AudioData is internal proprietary API and may be removedin a future release
Sound.java:25: warning: ContinuousAudioDataStream is internal proprietary API and may be removed in a future release
Sound.java:27: warning: AudioStream is internal proprietary API and may be removed in a future release
Sound.java:29: warning: ContinuousAudioDataStream is internal proprietary API and may be removed in a future release

Note: Sound.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

7 warnings
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
2.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

First, you should be using the classes in the javax.sound.sampled package to avoid the compiler warnings. Second, when using these classes, you'll also need to drive them from a thread in the background.

Here's one a wrote awhile ago. There are better ways to do it now than to sleep in a loop, but it works for quick and easy wav files, and you can adapt the code if you need to. A clever implementation might even be able to drive several audio files from the same thread.

Plays an audio file:

import java.io.InputStream;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;

public class SoundThread extends Thread {

    private final String resource;

    public static void play(String resource) {
        Thread t = new SoundThread(resource);
        t.setDaemon(true);
        t.start();
    }

    public SoundThread(String resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        Clip clip = null;
        try {
            InputStream in = SoundThread.class.getClassLoader().getResourceAsStream(resource);
            if(in != null) {
                AudioInputStream stream = AudioSystem.getAudioInputStream(in);
                AudioFormat format = stream.getFormat();
                DataLine.Info info = new DataLine.Info(Clip.class, format);
                clip = (Clip) AudioSystem.getLine(info);
                clip.open(stream);
                clip.loop(0);
                do  {
                    try {
                        Thread.sleep(100);
                    } catch(InterruptedException iex) {
                        // bad form on my part here, should do something
                    }
                } while(clip.isRunning());
            }
        } catch (Exception e) {
            x.printStackTrace(System.out);
        } finally {
            try {
                if(clip != null) {
                    clip.close();
                }
            } catch(Exception x) {
                x.printStackTrace(System.out);
            }
        }
    }
}

Example of how to call it:

SoundThread.play("resources/cashregister6.wav");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...