2

我正在使用 JAVA ME 1.4 和 WTK 和 LWUIT,并且希望在 PhoneME 中运行的 Java 应用程序中发出哔声。

我在谷歌上发现了几种方法,但没有一种方法有效。

我的最后一次尝试是使用Display.getInstance().playBuiltinSound(Display.SOUND_TYPE_ERROR)但没有成功。

另一个是AlertType.WARNING.playSound(Display.getDisplay(midlet)):也不工作

还有这个:用 J2ME 播放音频;没有成功

有人可以帮助找到一种在 JAVA ME 上播放哔声的通用方法吗?

4

1 回答 1

0

If it was me, I would use the Player object to play an AMR file, or a MIDI file.

AMR is the most widely supported streamed audio format for JavaME. You can convert WAV files to AMR easily using various converters.

Using the Player object is very straight forward. Here is an example for playing a MIDI. First create the Player object:

Player myPlayer = Manager.createPlayer(getClass().getResourceAsStream("music.mid"), "audio/midi"); // For AMR use audio/amr

After this you may be able to just call myPlayer.start(), but here's the trouble: Some devices require you to first call realize() and prefetch(), while these exact calls will mess up the playback on other devices. So to get playback working on most possible devices, you just throw in a few try/catch blocks:

try {
    myPlayer.realize();
} catch (Exception e) {} // Didn't work? Oh well, never mind.

try {
    myPlayer.prefetch();    
} catch (Exception e) {} // Again, we don't care if it didn't work.

try {
    myPlayer.start();   
} catch (Exception e) {}

Using that approach should give you the a working sound on most possible devices.

Finding a beep sound online to use shouldn't be a problem.

于 2012-10-31T19:38:26.603 回答