如何将声音与Codename One捆绑在一起? [英] How to bundle sounds with Codename One?

查看:72
本文介绍了如何将声音与Codename One捆绑在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Codename One应用程序中包含声音,以实现诸如单击按钮,过渡等效果.我不希望从URL下载声音,因为它们很小,并且我希望即使在设备处于播放状态时也可以播放声音未连接到互联网.看来我无法在主题中包含源文件.我该怎么办?

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?

推荐答案

将声音文件放入项目文件夹中的"src"文件夹,并按如下所示进行引用:

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");

这篇关于如何将声音与Codename One捆绑在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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