2017-06-20 41 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。我會請一個從事這項工作的人來研究它 –