gpt4 book ai didi

java - 如何使用javax.sound.sampled.LineListener?

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

我有一个程序,它会询问用户他们想要从可用歌曲列表中播放哪些歌曲,并且在用户选择一首歌曲后,一旦歌曲完成,它会询问用户他们想要再次播放哪首歌曲。我被告知为此使用线路监听器,但即使在使用 oracle 文档后我似乎也不知道如何使用

我的代码

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] pathnames;
File MusicFileChosen;
String musicDir;
boolean songComplete = false;

pathnames = ProgramMap.musicDir.list();

// Print the names of files and directories
for (int ListNum = 0; ListNum < pathnames.length; ListNum++) {
System.out.println(ListNum + 1 + ". " + pathnames[ListNum]);
}


for (int playlistLength = 0; playlistLength < pathnames.length; playlistLength++){
if (!songComplete) {
System.out.println("Which Song would you like to play?");
int musicChoice = input.nextInt();
musicDir = ProgramMap.userDir + "\\src\\Music\\" + pathnames[musicChoice - 1];
MusicFileChosen = new File(musicDir);
PlaySound(MusicFileChosen, pathnames[musicChoice - 1]);

}
}
}
public static void PlaySound(File sound, String FileName){
try{
// Inits the Audio System
Clip clip = AudioSystem.getClip();

AudioInputStream AudioInput = AudioSystem.getAudioInputStream(sound);

//Finds and accesses the clip
clip.open(AudioInput);

//Starts the clip
clip.start();

System.out.println("Now Playing " + FileName);

clip.drain();


}catch (Exception e){
System.out.println("Error playing music");
}

}}

最佳答案

基本上,您需要更改的一件事就是替换它:

for (int playlistLength = 0; playlistLength < pathnames.length; playlistLength++){

类似:

while (true) {
System.out.println("Which Song would you like to play?");
int musicChoice = input.nextInt();
musicDir = ProgramMap.userDir + "\\src\\Music\\" + pathnames[musicChoice - 1];
MusicFileChosen = new File(musicDir);
PlaySound(MusicFileChosen, pathnames[musicChoice - 1]);
}

您可以添加一些逻辑来打破循环。

此外,我建议稍微更改一下 PlaySound 方法:

    public static void PlaySound(File sound, String FileName) {
try (final AudioInputStream in = getAudioInputStream(sound)) {

final AudioFormat outFormat = getOutFormat(in.getFormat());
Info info = new Info(SourceDataLine.class, outFormat);

try (final SourceDataLine line =
(SourceDataLine) AudioSystem.getLine(info)) {

if (line != null) {
line.open(outFormat);
line.start();
System.out.println("Now Playing " + FileName);
stream(getAudioInputStream(outFormat, in), line);
line.drain();
line.stop();
}
}

} catch (UnsupportedAudioFileException
| LineUnavailableException
| IOException e) {
System.err.println("Error playing music\n" + e.getMessage());
}
}

private static AudioFormat getOutFormat(AudioFormat inFormat) {
final int ch = inFormat.getChannels();
final float rate = inFormat.getSampleRate();
return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
}

private static void stream(AudioInputStream in, SourceDataLine line)
throws IOException {
final byte[] buffer = new byte[4096];
for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) {
line.write(buffer, 0, n);
}
}

它需要播放MP3,因为你可能会遇到这样的问题: Unknown frame size .

要向 Java Sound 添​​加 MP3 读取支持,请将 JMF 的 mp3plugin.jar 添加到应用程序的运行时类路径中。 https://www.oracle.com/technetwork/java/javase/download-137625.html

关于java - 如何使用javax.sound.sampled.LineListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61019319/

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