title: How to play audio from server in android
body: <p>I am getting all audio file list from server to list-view by following code.</p>
  
  <pre><code>public class ServerFileList extends Activity {
  
      URL urlAudio;
      ListView mListView;
      ProgressDialog pDialog;
      private MediaPlayer mp = new MediaPlayer();
      private List&lt;String&gt; myList = new ArrayList&lt;String&gt;();
  
      @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&lt;?&gt; 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&lt;String, Integer, String&gt; {
          @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&lt;String, String, String&gt; {
          @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&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(
                      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);
          }
      }
  }
  </code></pre>
  
  <p>And the output is</p>
  
  <p><img src="https://i.stack.imgur.com/Vzjwl.png" alt="enter image description here"></p>
  
  <p>Now i want to play that audio file by clicking list-view for that i wrote code that already i have listed above but i am getting following error.</p>
  
  <p>I have referred below link for the reference.</p>
  
  <p><a href="https://stackoverflow.com/questions/20628061/how-to-play-audio-file-mp3-from-the-server">How to play Audio file Mp3 from the server</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7441140/how-to-play-audio-file-from-server-in-android">how to play audio file from server in android</a></p>
  
  <p>But in this both link they have specified particular audio file to play from server and given full path but i don't want to do that.</p>
  
  <p>I know it is clearly saying that I can not cast URL to String, But i don't know how can i solve this. please help.</p>
  
  <pre><code>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)
  </code></pre>