gpt4 book ai didi

java - AudioPlayer 是内部专有 API,可能会在未来版本中删除

转载 作者:行者123 更新时间:2023-12-01 22:04:07 31 4
gpt4 key购买 nike

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

当我运行此代码时,我看到以下警告。为什么以及如何避免这种情况?

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

最佳答案

首先,您应该使用 javax.sound.sampled 包中的类以避免编译器警告。其次,在使用这些类时,您还需要从后台线程驱动它们。

这是不久前写的一篇。现在有比循环 sleep 更好的方法来做到这一点,但它适用于快速且简单的 wav 文件,并且您可以根据需要调整代码。巧妙的实现甚至可能能够从同一线程驱动多个音频文件。

播放音频文件:

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

如何调用它的示例:

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

关于java - AudioPlayer 是内部专有 API,可能会在未来版本中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58702560/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com