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

warherolion:

我有一个程序,会问他们想要的歌曲,可播放歌曲列表出来,用户可以选择一个曾经歌曲结束后它会询问他们要再次播放哪首歌曲的用户的用户。我被告知要使用的线路监听器这一点,但我似乎无法如何,即使使用了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,因为你可以面对这样的问题:未知帧大小

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Java Sound时控制Clip的音量(javax,sound.sampled)

在“ javax.sound.sampled.AudioSystem”类型中找不到名为getClip的方法

Java getAudioInputStream 试图读取音频文件,得到 javax.sound.sampled.UnsupportedAudioFileException,

使用javax.sound.sampled.Clip播放,循环播放和停止游戏中的多种声音。意外错误

如何在Tensorflow中使用sampled_softmax_loss

尝试使用javax.sound注册语音

javax.sound.Midi,如何包含元事件类型的示例?

使用 javax.sound.midi 接收 MIDI 输入

使用javax.sound.midi包设置乐器通道

如何在Tensorflow Keras中使用tf.nn.sampled_softmax_loss?

导入javax.sound无法解析

OSX上带有Javax.sound.midi的Midi设备

如何从 R.raw 设置 Sound RingtoneManager

如何循環播放 sf::Sound?

如何使用Java Sound中的音频样本数据?

将Tensorflow Graph转换为使用Estimator,使用`sampled_softmax_loss`或`nce_loss`在损失函数中获取'TypeError:数据类型无法理解'

为什么 Tensorflow 的 sampled_softmax_loss 强制您使用偏差,而专家建议 Word2Vec 不使用偏差?

如何在 glsl 着色器中读取具有 R8_UINT 格式和 SAMPLED_IMAGE 描述符类型的 3D 图像?

如何重新启动Java Sound API中的音频播放?

使用Java Sound API播放MP3

不使用 react-native-sound 播放声音

使用 react-native-sound 的正确方法是什么?

MIDI乐器不适用于javax.sound.midi.MidiChannel

Java中的声音:javax.sound和sun.audio之间有什么区别?

libGDX和javax.sound有冲突吗?和规模问题

如何在 Vue 中导入和使用 P5.Sound?

如何使用Java Sound缓冲和播放OGG文件中的声音数据

除了javax.sound.midi外,Java中还有一个简单的免费MIDI实现:有吗?

在React和Gatsby.js中使用use-sound-如何在路线更改时停止播放音频?