2017-06-20 38 views
1

无论是在Android还是iOS上,都会播放LocalNotification实例引用的声音文件(请查看下面的示例代码)。在Android上播放系统的标准声音。任何人都知道我在这里做错了什么?CN1中的LocalNofication声音

@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在我的默认包确实存在!

+0

这看起来像Codename One中的一个错误。我在这里提出了一个问题(https://github.com/codenameone/CodenameOne/issues/2137)。目前看起来没有解决方法。 –

回答

0

如果你想从设备存储的声音文件,那么它不能直接检测 不使用该

ntf.setAlertSound("/" + soundFileName); 

的试试这个

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

这样,就会找到你的文件并且您的特定声音将会播放。还有一件事是在清单中添加“android.permission.READ_EXTERNAL_STORAGE”权限。 祝你好运。

+0

感谢您的快速反应,但这不是它可以在CN1中处理的方式。例如'Environmet'在CN1范围内不可用。此外[cn1DDoc](https://www.codenameone.com/javadoc/com/codename1/notifications/LocalNotification.html#setAlertSound-java.lang.String-)这样说 – Osman

0

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

+0

嗯我很困惑,因为当我通过一个完全合格的路径,我收到以下错误:“警报声音文件名必须以'notification_sound'前缀'开头(这也在方法的java文档中声明)。这就是我传递“/notification_sound_file.mp3”的原因。 – Osman

+0

对不起,我猜我并不熟悉那个API。我会请一个从事这项工作的人来研究它 –