我通过以下代码获取从服务器到列表视图的所有音频文件列表。
public class ServerFileList extends Activity {
URL urlAudio;
ListView mListView;
ProgressDialog pDialog;
private MediaPlayer mp = new MediaPlayer();
private List<String> myList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.serverfilelist);
mListView = (ListView) findViewById(R.id.listAudio);
new getAudiofromServer().execute();
new downloadAudioFromServer().execute();
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
playSong(urlAudio + myList.get(position));
}
});
}
private void playSong(String songPath) {
try {
mp.reset();
mp.setDataSource(songPath);
mp.prepare();
mp.start();
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
private class downloadAudioFromServer extends
AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... url) {
int count;
try {
URL url1 = new URL("http://i-qualtech.com/Fidol/uploadAudio");
URLConnection conexion = url1.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
InputStream input = new BufferedInputStream(url1.openStream());
OutputStream output = new FileOutputStream(
Environment.getExternalStorageDirectory() + "/Sounds/");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int) (total * 100 / lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;
}
}
class getAudiofromServer extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ServerFileList.this);
pDialog.setMessage("Getting File list from server, Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@SuppressWarnings("unchecked")
@Override
protected String doInBackground(String... arg0) {
try {
urlAudio = new URL("http://i-qualtech.com/Fidol/uploadAudio");
} catch (MalformedURLException e) {
e.printStackTrace();
}
ApacheURLLister lister1 = new ApacheURLLister();
try {
myList = lister1.listAll(urlAudio);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
ServerFileList.this, android.R.layout.simple_list_item_1,
myList);
adapter.notifyDataSetChanged();
mListView.setAdapter(adapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView.setCacheColorHint(Color.TRANSPARENT);
}
}
}
输出
现在我想通过单击列表视图来播放该音频文件,因为我编写的代码已经在上面列出,但我收到了以下错误。
我在下面提到了链接供参考。
How to play Audio file Mp3 from the server
how to play audio file from server in android
但在这两个链接中,他们指定了特定的音频文件从服务器播放并给出了完整路径,但我不想这样做。
我知道这显然是说我无法将URL转换为String,但我不知道如何解决这个问题。请帮忙。
02-05 01:44:58.690: E/AndroidRuntime(1998): FATAL EXCEPTION: main
02-05 01:44:58.690: E/AndroidRuntime(1998): java.lang.ClassCastException: java.net.URL cannot be cast to java.lang.String
02-05 01:44:58.690: E/AndroidRuntime(1998): at iqual.fidol_final.ServerFileList$1.onItemClick(ServerFileList.java:53)
02-05 01:44:58.690: E/AndroidRuntime(1998): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
02-05 01:44:58.690: E/AndroidRuntime(1998): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
02-05 01:44:58.690: E/AndroidRuntime(1998): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
02-05 01:44:58.690: E/AndroidRuntime(1998): at android.widget.AbsListView$1.run(AbsListView.java:3463)
02-05 01:44:58.690: E/AndroidRuntime(1998): at android.os.Handler.handleCallback(Handler.java:730)
02-05 01:44:58.690: E/AndroidRuntime(1998): at android.os.Handler.dispatchMessage(Handler.java:92)
02-05 01:44:58.690: E/AndroidRuntime(1998): at android.os.Looper.loop(Looper.java:137)
02-05 01:44:58.690: E/AndroidRuntime(1998): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-05 01:44:58.690: E/AndroidRuntime(1998): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 01:44:58.690: E/AndroidRuntime(1998): at java.lang.reflect.Method.invoke(Method.java:525)
02-05 01:44:58.690: E/AndroidRuntime(1998): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-05 01:44:58.690: E/AndroidRuntime(1998): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-05 01:44:58.690: E/AndroidRuntime(1998): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
为什么不首先通过将URL对象转换为String来准备要播放的正确URL?
改变这个: playSong(urlAudio + myList.get(position));
到
playSong(urlAudio。 toString() + myList.get(position));
答案 1 :(得分:0)
我不知道这段代码是否对你有帮助
但尝试一下:
在创建之前。
Uri uri;
添加到您的代码
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//playSong(urlAudio + myList.get(position));
uri = Uri.parse(urlAudio);
playSong(urlAudio + myList.get(position));
}
});
添加到playSong:
private void playSong(String songPath) {
try {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(this, uri);
mp.prepare();
mp.start();
}
catch (NullReferenceArgument e) {
Log.d(TAG, "NullReferenceException: " + e.getMessage());
}
catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException: " + e.getMessage());
}
catch (IOException e) {
Log.d(TAG, "IOException: " + e.getMessage());
}
catch (IllegalArgumentException e) {
Log.d(TAG, "IllegalArgumentException: " + e.getMessage());
}
catch (SecurityException e) {
Log.d(TAG, "SecurityException: " + e.getMessage());
}
答案 2 :(得分:0)
我设法解决了这个问题,只需将下面一段代码添加到我的onItemClicklistner中,它现在工作正常,
String url = null;
Object o = myList.get(position);
url = o.toString().replace(" ", "%20").trim();
play = (PlaySongAsy) new PlaySongAsy(url).execute();