Download Audio File From Url To Storage And Play On Download Complete In Codename One

牧云@^-^@ 提交于 2019-12-25 04:44:13

问题


I am developing an application using codenameone that needs to download an audio file (mp3) from a remote server, and then play it once the download is complete.

I want it to happen once a button is clicked, but so far...all i see in the simulator is the infinite dialog and nothing else.

I am using java 7 on my development pc.

Here are relevant code snippets:

          ok.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent ae) {
        String file=_cat+".mp3";
        String path="/"+file;
        Util.downloadUrlToStorage("http://abcde.com/images/"+file,path,true);

       final InputStream is = Display.getInstance().getResourceAsStream(this.getClass(), path);
    try {
      Media  mm = MediaManager.createMedia(is, "audio/mp3");
        mm.play();
    } 
    catch (IOException ex) {
       ex.printStackTrace();
    }
    }
          });

How can i get to download the audio file and play it once it completes?. I edited my code in line with shai almog's answer, to the following:

          ok.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent ae) {
               String file=_cat+".mp3";
        Util.downloadUrlToStorage("http://abcde.com/images/"+file,file,true);


        try {
            is = Storage.getInstance().createInputStream(file);
        } catch (IOException ex) {
           ex.printStackTrace();
        }
    try {
      Media  mm = MediaManager.createMedia(is, "audio/mp3");
        mm.play();
    } 
    catch (IOException ex) {
       ex.printStackTrace();
    }

....and still nothing gets downloaded....i go to the

         .CodenameOneStorage 

directory on my PC but nothing there..i guess i am missing something


回答1:


getResourceAsStream returns a stream into a jar and is a read only path. Its not a "file" and you can't write there. Storage is a simplified file system that is unrelated to that either.

You can use something like:

Util.downloadUrlToStorage("http://abcde.com/images/"+file,file,true);
InputStream is = Storage.getInstance().createInputStream(file);


来源:https://stackoverflow.com/questions/31183793/download-audio-file-from-url-to-storage-and-play-on-download-complete-in-codenam

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