从URL将音频文件下载到存储区并播放,并以代号1完成下载 [英] Download Audio File From Url To Storage And Play On Download Complete In Codename One
问题描述
我正在开发一个使用codenameone的应用程序,该应用程序需要从远程服务器下载音频文件(mp3)
,然后在下载完成后播放它。
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.
我在开发PC上使用的是Java 7。
I am using java 7 on my development pc.
以下是相关的代码段:
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();
}
}
});
如何完成音频文件的下载和播放?
我按照shai almog的答案编辑了以下代码:
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();
}
....仍然没有下载.... i进入
....and still nothing gets downloaded....i go to the
.CodenameOneStorage
目录在我的PC上,但是没有。.我猜
i不见了
directory on my PC but nothing there..i guess i am missing something
推荐答案
getResourceAsStream
将流返回到jar中,并且是只读路径。它不是文件,您不能在此处写。存储是一种与此无关的简化文件系统。
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);
这篇关于从URL将音频文件下载到存储区并播放,并以代号1完成下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!