- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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/
当我在 iOS 上测试它时,它工作正常,但是当我尝试在 XCode 上存档时,它给出以下错误: Module 'audioplayers' not found 我使用audioplayers 0.13
我想使用 quiver async 中的节拍器类和 audioplayers 包在 Flutter 中编写一个节拍器应用程序。但是,音频播放器发出的咔嗒声总是延迟播放。有时会有几个小节没有声音,然后是
我将程序的文本复制到另一台计算机,但导入 sun.audio.AudioPlayer 时出现问题类(class)。编译器在导入字符串下划线,当我将鼠标放在它上面时,有这段文字,我不太明白: Acces
我在MATLAB中使用sound()函数生成音调。以下功能会以440Hz的频率播放4秒钟的声音: duration = 4 toneFreq = 440 samplesPerSecond = 4410
我在 flutter 中使用 audioplayers 库,我试图保存和恢复播放器位置,除了从第一个位置播放,就像缓存播放器搜索栏位置一样,在这段代码中,我尝试使用 SharedPreference
我是Flutter的初学者。 我正在使用Audioplayers Flutter库来播放assets/sounds/my_sound.mp3中的素材声音。我正在这样使用AudioCache: Audi
我正在尝试使用 audioplayers flutter 插件在以下链接播放广播 https://video2b.vixtream.net/radio/v/fbcfm 我尝试设置网址,但这不起作用。
我如何可以多次播放同一音频文件,而不是循环播放,但我希望能够精确地播放多少次,所以我遵循此tutorial进行了基础(即播放/暂停功能)。我正在为其余的工作而苦苦挣扎。 我使用了audioplayer
为什么以下代码对我不起作用? :S AudioPlayer.player.start(file1); while(AudioPlayer.player.isAlive()) {} AudioPlaye
我发现使用 AVFoundation 可以直接播放单轨道,但在查看苹果指南后,我似乎无法播放多首轨道。我做这项工作哪里出错了?下面的 queuePlayer 是 AVQueuePlayer 的实例,据
这里有点不知所措。我正在使用 Xcode 7 和 Swift 2,我正在尝试寻找实际有效的流式音频示例。 本质上我希望能够流式传输,但我也试过这个:Swift Radio Streaming AVPl
我在我的应用程序中使用包 flutter 音频播放器来播放来自谷歌云 tts 的音频。 它在 iOS 模拟器上运行良好,但是当我构建 apk 时出现以下错误: e: /Users/josuha/Doc
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 8年前关闭。 Improve this questi
import javax.swing.*; import sun.audio.*; import java.awt.event.*; import java.io.*; public class So
我正在尝试使用 flutter 的音频播放器库播放简单的声音。当我尝试这样做时,出现了无法加载 Assets 错误。我已经在这个站点上查看了几个相关问题,所有这些问题似乎都只建议检查 pubspec.
在我的 swing 应用程序中,我使用类在单击鼠标时播放声音。我遇到的问题是,当我调用类(class)时,声音只播放一次,而当突然单击另一个按钮时,它不会播放声音。我尝试在我的代码中延迟,但 id 仍
import javax.swing.*; import sun.audio.*; import java.awt.event.*; import java.io.*; public class So
我正在使用以下代码将声音文件播放两次。第二个声音在第一个声音之后立即播放 是否可以在它们之间留出时间间隔(1 秒)?我试图通过阅读 Apple Docs 找到解决方案 var letterSound:
我有一个在 AVAudioPlayer 中播放的主菜单声音文件。 我想创建一个选项按钮,将我带到另一个 View Controller 。当我单击开关以选择“关闭”主菜单声音时。如何停止播放音频? 如
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我是一名优秀的程序员,十分优秀!