我正在尝试使用以下内容在程序的后台进行简单的mp3播放:
Media med = new Media(getClass().getResource("intro.mp3").toExternalForm());
MediaPlayer mPlayer = new MediaPlayer(med);
mPlayer.play();
Run Code Online (Sandbox Code Playgroud)
intro.mp3文件与其他.class文件一起放在我的包的bin文件夹中.
问题是我的程序终止于:
Exception in thread "main" java.lang.IllegalStateException: Toolkit not initialized
Run Code Online (Sandbox Code Playgroud)
完整终止日志是:
Device "Intel(R) HD Graphics Family" (\\.\DISPLAY1) initialization failed :
WARNING: bad driver version detected, device disabled. Please update your driver to at least version 8.15.10.2302
Exception in thread "main" java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:153)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:148)
at javafx.application.Platform.runLater(Platform.java:52)
at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:450)
at javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:365)
at PokerApp.<init>(PokerApp.java:33)
at PokerApp.main(PokerApp.java:105)
Run Code Online (Sandbox Code Playgroud)
根据问题的原因,任何人都有任何想法吗?
我正在使用SMS处理,基于Java的软件,并希望在收到消息时发出哔哔/警报声.我试着查看java.sound库,找不到任何东西.我不知道在Java应用程序中播放声音的小程序是否合适!在我们可以在应用程序中调用的任何Java库中是否有任何预定义的声音?任何指针将不胜感激!
我正试着玩
PCM_UNSIGNED 11025.0 Hz, 8 bit, mono, 1 bytes/frame
Run Code Online (Sandbox Code Playgroud)
第一种方法有效,但我不想依赖于sun.*
东西.第二个导致只播放一些领先的帧,听起来更像是一个点击.因为我正在使用ByteArrayInputStream播放,所以不能成为IO问题.
Plz分享您为什么会发生这种情况的想法.TIA.
我是一名学习 Java 的学生,正在为我的简历做一个独立的项目。我决定做一个 Java 计算器,因为我知道组成它的大部分组件。我不知道该怎么做的一件事是在按钮按下时添加声音。我对 Audiostream 输入的内容有一个模糊的想法。但是我的计算器上的每个按钮都需要独特的声音。现在我的计算器还没有完全完成(监听器还没有工作。)我只是想知道将我的 .wav 文件合并到按钮按下的最佳方法是什么。提前致谢。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator {
//instance variables
JFrame frame;
JPanel mainPanel, northPanel, southPanel;
JTextField numberLabel;
JButton backspace, multiply, divide, add, subtract, equal; //four function buttons
JButton one, two, three, four, five, six, seven, eight, nine, zero; //number buttons
JButton posOrNeg, decimal, leftParenthese, rightParenthese;
//constructor
public Calculator()
{
//create it
frame = new JFrame();
mainPanel = new JPanel(); //contains both panels
mainPanel.setForeground(Color.BLACK);
mainPanel.setBackground(Color.DARK_GRAY);
northPanel = new JPanel(new …
Run Code Online (Sandbox Code Playgroud) 我正在使用InputStream对象来计算某些文件的Md5。我标记了流,稍后重置流。但是,对于大文件,将出现以下异常...
inStreamLatestFile.mark(0);
checkSumCalculated = MD5CheckSumCalculator.calculateMD5CheckSum(inStreamLatestFile);
inStreamLatestFile.reset();
Run Code Online (Sandbox Code Playgroud)
例外
.Md5ValidationAggrStrat ||**Error in calculating checksum:: java.io.IOException: Resetting to invalid mark**
||java.io.IOException: Resetting to invalid mark
||at java.io.BufferedInputStream.reset(BufferedInputStream.java:437)
||at com.amadeus.apt.ib.modules.func.map.camel.strategy.Md5ValidationAggrStrategy.aggregate(Md5ValidationAggrStrategy.java:81)
||at org.apache.camel.processor.aggregate.AggregateProcessor.onAggregation(AggregateProcessor.java:365)
||at org.apache.camel.processor.aggregate.AggregateProcessor.doAggregation(AggregateProcessor.java:245)
||at org.apache.camel.processor.aggregate.AggregateProcessor.process(AggregateProcessor.java:201)
||at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
||at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
Run Code Online (Sandbox Code Playgroud)
我试图关闭流并以这种方式重新打开它。.只是为了获得一些异常,如下所示:
try {
inStreamLatestFile= ExchangeHelper.getMandatoryInBody(
oldExchange, InputStream.class);
//inStreamLatestFile.mark(0);
checkSumCalculated = MD5CheckSumCalculator.calculateMD5CheckSum(inStreamLatestFile);
//closing the inputStream of the latest file
if(inStreamLatestFile != null){
try {
inStreamLatestFile.close();
} catch (IOException e) {
logger.error("Error occurred in closing the stream :: "+ e.getMessage());
}
}
tempInputStream= ExchangeHelper.getMandatoryInBody(
oldExchange, InputStream.class); …
Run Code Online (Sandbox Code Playgroud) 我正在创建一个短片然后播放它,调用该start()
方法.然后我调用该drain()
方法来阻止执行,直到剪辑回放完成.但是,当运行多次下面的代码时,有时它会工作,有时它不会,并且声音在结束前随机停止.
Mixer mixer = AudioSystem.getMixer(null);
AudioFormat format = new AudioFormat(44100, 8, 1, true, false);
DataLine.Info info = new DataLine.Info(Clip.class, format);
try {
// Create a sound of 1 second
Clip clip = (Clip)mixer.getLine(info);
byte[] b = new byte[44100];
for(int i=0; i<b.length; i++)
b[i] = (byte) (50*Math.sin(i/10.0));
clip.open(format, b, 0, b.length);
clip.setFramePosition(0);
clip.start();
clip.drain();
} catch(LineUnavailableException lue) { lue.printStackTrace(); }
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:这是一个错误吗?还是我误解了这个drain()
方法?
我有一个小程序,从数据线麦克风读取到字节数组我不是100%确定它的工作原理,但只是通过打印部分数组,它似乎在麦克风旁边发出声音时变化:)
我喜欢播放声音如何从缓冲区播放数据(字节数组到声音)?
package mic;
import javax.sound.sampled.*;
public class Mic extends Thread{
boolean flag;
TargetDataLine mic;
byte[] buffer;
AudioFormat format;
public static void main(String[] args) {
Mic a=new Mic();
a.start();
}
@Override
public void run()
{
flag=true;
startMic();
while(flag)
{
send();
}
}
public void send()
{
try{
mic.read(buffer,0,512);
System.out.println("1. "+buffer[0]);
}catch(Exception ex)
{
}
}
public void startMic()
{
try{
format=new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,8000.0F,16,2,4,8000.0F,true);
DataLine.Info info=new DataLine.Info(TargetDataLine.class,format);
mic=(TargetDataLine)AudioSystem.getLine(info);
mic.open();
mic.start();
buffer=new byte[512];
}catch(Exception exx)
{
System.out.println("exx");
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我最终想要做的是将字节数组发送到其他应用程序并立即播放,
如收音机的直播
我有一个应用程序,我将在Linux touch系统上安装.触摸系统没有给我触摸声,所以我决定在我的应用程序上有这个功能.最好的方法是什么?我不想浏览每个按钮和其他组件并在那里编写代码.是否有任何全局方式来处理它,以便在触摸屏幕或点击鼠标时声音在整个应用程序中工作?
每当我在游戏中播放声音时,线程会在声音播放结束后冻结然后继续.声音引擎代码:
package com.kgt.platformer;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
public class Sound {
private final static int BUFFER_SIZE = 12800000;
private static File soundFile;
private static AudioInputStream audioStream;
private static AudioFormat audioFormat;
private static SourceDataLine sourceLine;
/**
*
* @param filename the name of the file that is going to be played
*
*/
public static void playSound(String filename){
String strFilename = filename;
try {
soundFile = new File(strFilename);
} …
Run Code Online (Sandbox Code Playgroud)