【发布时间】:2014-02-05 07:06:46
【问题描述】:
我正在通过以下代码从服务器获取所有音频文件列表到列表视图。
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 转换为字符串,但我不知道如何解决这个问题。请帮忙。
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)
【问题讨论】:
标签: java android audio android-listview classcastexception