gpt4 book ai didi

java - 在 Java 中,使用剪辑播放音频无法正常工作且没有错误消息

转载 作者:行者123 更新时间:2023-12-03 02:00:54 25 4
gpt4 key购买 nike

我一直在尝试使用 Oracle 的“Trail: Sound”,我已经到了这一部分。 https://docs.oracle.com/javase/tutorial/sound/playing.html#113609
“使用剪辑”

好吧,我尝试遵循不完全具体的指示,并编写了一些看起来应该可以工作的代码。它基本上与我在网上找到的对其他人有用的示例相匹配。在我的机器上没有任何 react ,程序在我启动后没有播放任何声音就结束了。它没有说任何错误。

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Clip;
import javax.sound.sampled.AudioFormat;
import java.io.File;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
class Demo{
public static void main(String[] args) throws LineUnavailableException, UnsupportedAudioFileException, IOException{
File file = new File("song1.wav");
AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(file);
AudioFormat audioFormat = audioFileFormat.getFormat();
javax.sound.sampled.DataLine.Info dataLineInfo = new javax.sound.sampled.DataLine.Info(Clip.class,audioFormat);
Line theLine = AudioSystem.getLine(dataLineInfo);
Clip clip = (Clip)theLine;
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
clip.open(audioInputStream);
clip.start();
}
}

编辑:我在 clip.start() 之后发现我需要保持程序打开。

最佳答案

我使用该类来播放 MP# 声音并为我工作

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javazoom.jl.player.Player;
import monitorbixao.Main;

/**
*
* @author C007329
*/
public class MusicPlayer extends Thread {

private File music;
private Player player;

public MusicPlayer(File music) {
this.music = music;
}


@Override
public void run() {
play();
}

public void play() {
try {
FileInputStream stream = new FileInputStream(music);
BufferedInputStream buffer = new BufferedInputStream(stream);
this.player = new Player(buffer);
//System.out.println("Executando...");
this.player.play();
//System.out.println("Terminado");
} catch (Exception e) {
//System.out.println("Erro!");
Main.logApp.addMsgLog(MusicPlayer.class.getCanonicalName(), e.getMessage());
e.printStackTrace();
}
}

public void close() {
this.player.close();
//System.out.println("Interrompido...");
}
}

关于java - 在 Java 中,使用剪辑播放音频无法正常工作且没有错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31621989/

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