CN1中的LocalNofication声音

时间:2017-06-20 08:50:05

标签: android ios codenameone

在Android和iOS上都没有播放LocalNotification实例引用的声音文件(请查看下面的示例代码)。在Android上播放系统的标准声音。谁知道我在这里做错了什么?

@Override
protected void postMain(Form f) {

    Container contentPane = f.getContentPane();
    contentPane.setLayout(new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE));
    contentPane.setUIID("ToastBar");

    Button btn = new Button("create Notification");
    btn.addActionListener((e) -> {
        scheduleNotification(Display.getInstance(), 0, "Title", "Body", "notification_sound_file.mp3", System.currentTimeMillis() + 1000);
    });

    contentPane.add(BorderLayout.CENTER, btn);
}

private void scheduleNotification(Display dsp, int nId, String nTitle, String nBody, String soundFileName, long scheduleTime) {

    LocalNotification ntf = new LocalNotification();
    ntf.setId(nId + " " + scheduleTime);
    ntf.setAlertTitle(nTitle);
    ntf.setAlertBody(nBody + " " + scheduleTime);
    ntf.setAlertSound("/" + soundFileName);
    Log.p("scheduling ntf ("+nTitle+","+ ntf.getAlertBody() +","+ ntf.getAlertSound());
    dsp.scheduleLocalNotification(ntf, scheduleTime, LocalNotification.REPEAT_NONE);
}

注意: notification_sound_file.mp3 确实存在于我的默认包中!

2 个答案:

答案 0 :(得分:0)

如果您想从设备存储中获取声音文件,则无法直接检测 而不是使用这个

ntf.setAlertSound("/" + soundFileName);

试试这个

ntf.setAlertSound(Environment.getExternalStorageDirectory().getAbsolutePath() + "Folder_Name/" + soundFileName);

这将找到您的文件,您的特定声音将播放。还有一件事是添加" android.permission.READ_EXTERNAL_STORAGE"清单中也允许。 祝你好运。

答案 1 :(得分:0)

声音文件必须是FileSystemStorage中文件的路径,该文件必须始终完全合格。您需要将文件理想地复制到文件系统的app目录下,参见https://www.codenameone.com/how-do-i-use-storage-file-system-sql.html