How to bundle sounds with Codename One?

蓝咒 提交于 2019-12-11 00:47:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!