问题
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