gpt4 book ai didi

java - Head First Java——创建第一个音乐播放器报错

转载 作者:行者123 更新时间:2023-11-29 07:51:49 25 4
gpt4 key购买 nike

我在按照 HeadFirstJava 配方创建一个非常小的音乐播放器时遇到了麻烦。我按照书中的代码进行了操作,但它仍然存在一些错误......当我第一次编译它时,它给了我这个错误:

Dez 15, 2013 4:13:02 PM java.util.prefs.WindowsPreferences WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.

After googling the error I found out that I should create HKEY_LOCAL_MACHINE\Software\JavaSoft\Prefs and also give full permision for JavaSoft on regedit. That did solve the problem but only partially. The code compliles, the sound is made by the computer but the program will not close util I hit CTRL + C. Here is the code:

import javax.sound.midi.*;//importam pachetul sound.mini 

public class MiniMiniMusicApp {

public static void main (String [] args) {

MiniMiniMusicApp mini = new MiniMiniMusicApp();
mini.play();
} //inchidem main

public void play() {

try {

Sequencer player = MidiSystem.getSequencer();
player.open();

Sequence seq = new Sequence(Sequence.PPQ, 4);

Track track = seq.createTrack();

//ShortMessage first = new ShortMessage();
//first.setMessage(192, 1, 102, 0);
//MidiEvent noteOn1 = new MidiEvent(first, 1);
//track.add(noteOn1);


ShortMessage a = new ShortMessage();
a.setMessage(144, 1, 44, 100);
MidiEvent noteOn = new MidiEvent(a, 1);
track.add(noteOn);

ShortMessage b = new ShortMessage();
b.setMessage(128, 1, 44, 100);
MidiEvent noteOff = new MidiEvent(b, 16);
track.add(noteOff);

player.setSequence(seq);

player.start();

} catch (Exception ex) {
ex.printStackTrace();
}
} //inchidem play
}

我想提一下,我没有使用任何 GUI,而且我完全是个新手。任何帮助,将不胜感激。谢谢。

最佳答案

MIDI 音序器是一个在后台运行的特殊线程。只要它处于 Activity 状态(或者实际上,任何非守护线程处于 Activity 状态),Java 就不会自行退出。

尝试在 player.start(); 行之后添加:

Thread.sleep(5000);
player.close();

关于java - Head First Java——创建第一个音乐播放器报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20595399/

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