Java Sound API-扫描Midi设备 [英] Java sound api - Scanning for midi devices

查看:102
本文介绍了Java Sound API-扫描Midi设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个Java项目,该项目使用javax.sound.midi库从Midi硬件接收midi事件.在文档中,它说MidiSystem.getMidiDeviceInfo()返回所有已连接的Midi硬件的列表.它对我有用,但问题是,它只能工作一次.第一次实际扫描设备会花费一些时间,但是之后每次扫描都会立即返回相同的列表,即使已连接新设备也是如此.有没有办法强迫它重新扫描?如果应用程序重新启动,它将重新扫描,但是我不希望用户连接新的Midi设备时必须重新启动.

I'm working on a java project that receives midi events from midi hardware using the javax.sound.midi library. In the documentation, it says that MidiSystem.getMidiDeviceInfo() returns a list of all connected midi hardware. It works for me, but the problem is, it only works once. It takes a moment the first time to actually scan for the devices, but each time after that it will immediately return that same list even if new devices have been connected. Is there a way to force it to rescan? It will rescan if the application is restarted, but I don't want my users to have to restart if they connect a new midi device.

顺便说一句,我使用的是Mac OS X ...已经指出,对于不同的OS,其行为可能有所不同.

BTW, I'm using Mac OS X... it's been pointed out that behavior may be different for different OS's.

推荐答案

MidiSystem.getMidiDeviceInfo()获取完整的提供程序列表,并从每个提供程序中提取设备的信息.

The MidiSystem.getMidiDeviceInfo() gets the full providers list, and extracts the info of the device from each provider.

从JDK底层类 com.sun.media.sound.JDK13Services ,通过静态方法getProviders()

The MIDIs provider list is recovered from the JDK underlaying class com.sun.media.sound.JDK13Services, through the static method getProviders()

公共静态同步列表getProviders(Class serviceClass) 获取包含已安装列表 提供者的实例 请求的服务.的清单 提供商在以下时间段内被缓存 cachingPeriod给定的时间.期间 在此期间,相同的List实例是 返回相同类型的 提供者.在此期间之后, 实例被构造并返回. 返回的列表是不可变的.

public static synchronized List getProviders(Class serviceClass) Obtains a List containing installed instances of the providers for the requested service. The List of providers is cached for the period of time given by cachingPeriod . During this period, the same List instance is returned for the same type of provider. After this period, a new instance is constructed and returned. The returned List is immutable.

因此,此类似乎在缓存中保存了Providers列表,在一定时间后将重新加载.您可以使用方法setCachingPeriod(int seconds)将此时间段设置为自定义值.据我了解,默认缓存时间设置为60秒.

So, it seems that this class holds thee Providers list in a cache, wich will be reloaded after a certain period. You can set this period to a custom value using the method setCachingPeriod(int seconds). As long as I know, the default caching period is set to 60 seconds.

例如,要每秒刷新一次此缓存,您可以将以下行添加到代码中:

As an example, to refresh this cache every second, you coud add this line to your code:

com.sun.media.sound.JDK13Services.setCachingPeriod(1);

请注意,该解决方案利用了Sun专有类,因此不可能100%可移植.

Please, note that this solution makes use of a Sun propietary class, so it could not be 100% portable.

这篇关于Java Sound API-扫描Midi设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆