gpt4 book ai didi

java - 如何调用我的音乐类并在 while 循环内播放音乐,该循环在调用音乐类的行下执行我的行?

转载 作者:行者123 更新时间:2023-12-01 18:34:02 25 4
gpt4 key购买 nike

我有两个类,我想知道如何调用我的音乐类(MP3 class2)以使歌曲在第一类的 while 循环中播放。我的问题是当我使用 MP3.main(args); 时在第一类中,第一类中的 while 循环不执行 MP3.main(args); 行以下的内容。因此,不会显示文本“输入命令”。我想我需要重新创建我的 MP3 类并传递参数(如果这是正确使用术语的话)。抱歉我对java还是个新手。

请注意,我已从第一类中删除了许多代码,因为它是用于项目的。

如果有人问我为什么不删除第一类并将文本移至第二类,长话短说,这不是一个选择。

**CLASS 1 ** 尝试播放第二类中的音乐,但程序未执行 MP3.main(args); 行以下的内容在我的 while 循环中。

public class ClassTest {
public static void main(String[] args) {
// create the loop
while (true) {
MP3.main(args);
System.out.println("Enter a command: ");
}
}
}

2 级 MP3 级

import javazoom.jl.player.Player;

import java.io.BufferedInputStream;
import java.io.FileInputStream;


class MP3 {
private static String filename = null;

// constructor that takes the name of an MP3 file
public MP3(String filename) {
this.filename = filename;
}

// play the MP3 file to the sound card
public static void play() {
try {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
Player player = new Player(bis);
player.play();
}
catch (Exception e) {
System.out.println("Problem playing file " + filename);
System.out.println(e);
}
}


// test client
public static void main(String[] args) {
String filename = ("Growth.mp3");
MP3 mp3 = new MP3(filename);
mp3.play();

try {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
Player player = new Player(bis);
player.play();
}
catch (Exception e) {
System.out.println("Problem playing file " + filename);
System.out.println(e);
}
}

}

最佳答案

查看 play() 方法可以发现这是一个阻塞操作。您可以在单独的线程中调用该方法,这样您就可以测试玩家何时完成并重新调用 play 方法,如下所示:

MP3.java

import javazoom.jl.player.Player;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MP3 {

private final String filename;

private ExecutorService executorService;
private Player player;

public MP3(String filename) {
this.filename = filename;
}

public void play() {
try {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
player.play();
} catch (Exception e) {
System.out.println("Problem playing file " + filename);
e.printStackTrace();
}
}

public void playAsync() {
if (executorService == null) {
executorService = Executors.newSingleThreadExecutor();
executorService.submit(this::play);
}
}

public void playForever() {
while (true) {
sleep();
if (player == null || player.isComplete()) {
System.out.println("Still playing: " + filename);
stopAsync();
playAsync();
}
}
}

public void stopAsync() {
if (player != null) {
player.close();
player = null;
}
if (executorService != null) {
executorService.shutdown();
executorService = null;
}
}

private void sleep() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

DemoApplication.java

public class DemoApplication {

public static void main(String[] args) {
String filename = ("Growth.mp3");
MP3 mp3 = new MP3(filename);
mp3.playForever();
}
}

关于java - 如何调用我的音乐类并在 while 循环内播放音乐,该循环在调用音乐类的行下执行我的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60098776/

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