简体   繁体   English

Head First Java-创建第一个音乐播放器错误

[英]Head First Java - creating the first music player error

I am having trouble with creating a very small music player following the HeadFirstJava recipe. 我在按照HeadFirstJava食谱创建一个非常小的音乐播放器时遇到了麻烦。 I followed the code in the book but it still has some bugs... When I first compiled it it gave me this error: 我遵循了本书中的代码,但是它仍然存在一些错误……当我第一次编译它时,它给了我这个错误:

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. 搜寻错误之后,我发现我应该创建HKEY_LOCAL_MACHINE\\Software\\JavaSoft\\Prefs并在regedit上对JavaSoft给予完全的权限。 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 . 代码符合要求,声音是由计算机发出的,但是如果我按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
} 

I would like to mention that I am not using any GUI and that I am a total novice. 我想提一下,我没有使用任何GUI,而且我是一个新手。 Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

The MIDI sequencer is a special thread that runs in the background. MIDI音序器是在后台运行的特殊线程。 As long as it is active (or, in fact, any non-daemon thread is active), Java will not exit on its own. 只要它是活动的(或者实际上任何非守护进程线程都是活动的),Java都不会自行退出。

Try adding this after the player.start(); 尝试在player.start();之后添加它player.start(); line: 线:

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

Command Prompt doesn't support the multi-programming. 命令提示符不支持多重编程。 So when you run the above program, the program is in the running state, after it's play() method and wait for the some event to occur(like another Framed based program in java). 因此,当您运行上述程序时,该程序处于play()方法之后,处于运行状态,并等待某个事件发生(例如Java中另一个基于框架的程序)。 you can write System.exit() after putting some delay(so that your voice would come). 您可以在放置一些延迟后编写System.exit()(这样您的声音就会响起)。 Currently you are killing the process from the DOS. 当前,您正在从DOS中终止该进程。

The docs [MidiDevice.open()] for player.open() says: 用于player.open()文档[MidiDevice.open()]说:

An application opening a device explicitly with this call has to close the device by calling close. 使用此调用显式打开设备的应用程序必须通过调用close来关闭设备。 This is necessary to release system resources and allow applications to exit cleanly. 这是释放系统资源并允许应用程序正常退出所必需的。

So you may use a try-with-resource-statement (Java 7+) to close it safely and wait gracefully (at the end of your try-block) for your track to finish: 因此,您可以使用try-with-resource-statement (Java 7+)安全地关闭它,并优雅地等待(在try-block的末尾),以完成跟踪:

try (Sequencer player = MidiSystem.getSequencer()) {
    ...

    while (player.isRunning()) {
        Thread.sleep(100);
    }
} catch (Exception ex) {
    ex.printStackTrace();
}

Prior to Java 7 you would have called player.close(); 在Java 7之前,您应该调用player.close(); in a finally-block attached to your try-catch-statement. 在附加到try-catch-statement的final块中。

Note : The access warning for the root users Preferences could have been suppressed by: 注意 :可以通过以下方式禁止root用户Preferences ”的访问警告:

PlatformLogger.getLogger("java.util.prefs")
        .setLevel(PlatformLogger.Level.SEVERE);

要解决第一个错误,只需以管理员身份运行编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM