问题
I want to include sounds in my Codename One app for effects like clicking a button, transitions, etc. I prefer not to download them from URL, since they are very small and I want them to be played even when the device is not connected to the internet. It looks like I cannot include the source files in the theme. What should I do?
回答1:
Put the sound file in the "src" folder inside your project folder and reference it like below:
private Media MEDIA = null;
public void playAudio(String fileName) {
try {
if (MEDIA == null) {
final InputStream is = Display.getInstance().getResourceAsStream(getClass(), "/" + fileName);
MEDIA = MediaManager.createMedia(is, "audio/mp3", new Runnable() {
@Override
public void run() {
MEDIA = null;
}
});
}
if (MEDIA != null && MEDIA.isPlaying() == false) {
MEDIA.setVolume(100);
MEDIA.play();
}
} catch (IOException ioe) {
}
}
...
playAudio("my_sound.mp3");
来源:https://stackoverflow.com/questions/40029586/how-to-bundle-sounds-with-codename-one