title: playing audio/video file from server using list-view in android
body: <p>I am trying to list all audio file from my HTTP server to list-view. I am successfully able to do this with below code.</p>
  
  <pre><code>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://serverlink/folder/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>So to do this i have imported <code>ivy</code> Apache external library. Now what i want to do is when user click's on any list item it should play that audio file over there. For that i have implemented this below code.</p>
  
  <p>I have declared </p>
  
  <pre><code>    Uri uri;
      URL urlAudio;
      ListView mListView;
      PlaySongAsy play;
      MediaPlayer mp = new MediaPlayer();
      private List&lt;String&gt; myList = new ArrayList&lt;String&gt;();
  
  mListView = (ListView) findViewById(R.id.listAudio);
  </code></pre>
  
  <p>and then</p>
  
  <pre><code>mListView.setOnItemClickListener(new OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) {
                  play = (PlaySongAsy) new PlaySongAsy(myList.get(position)
                          .replace(" ", "%20").trim()).execute();
              }
          });
  </code></pre>
  
  <p>then here is my <strong>PlaySongAsy</strong> class</p>
  
  <pre><code>class PlaySongAsy extends AsyncTask&lt;String, Void, Boolean&gt; {
  
          String baseURL;
  
          public PlaySongAsy(String baseURL) {
              this.baseURL = baseURL;
          }
  
          @Override
          protected void onPreExecute() {
              // TODO Auto-generated method stub
              super.onPreExecute();
              pDialog = ProgressDialog.show(ServerFileList.this,
                      "    Buffering...", "please wait..", false);
              pDialog.setCancelable(false);
  
          }
  
          @Override
          protected Boolean doInBackground(String... urls) {
              new Thread() {
                  @Override
                  public void run() {
                      play(baseURL);
                  }
              }.start();
              return true;
          }
  
          @Override
          protected void onPostExecute(Boolean result) {
              super.onPostExecute(result);
              // progressDialog.dismiss();
          }
      }
  </code></pre>
  
  <p>and at last <strong>Play</strong> method.</p>
  
  <pre><code>private void play(String baseURL) {
          Uri myUri = Uri.parse(baseURL);
          try {
              if (mp == null) {
                  this.mp = new MediaPlayer();
              } else {
                  mp.stop();
                  mp.reset();
              }
              mp.setDataSource(this, myUri); // Go to Initialized state
  
              mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
              mp.setOnPreparedListener(this);
              mp.setOnBufferingUpdateListener(this);
              mp.setOnErrorListener(this);
  
              mp.prepareAsync();
  
              // mp.setVolume(5.F, 5.F);
  
              Log.d("", "LoadClip Done");
          } catch (Throwable t) {
              Log.d("", t.toString());
          }
      }
  </code></pre>
  
  <p>while trying this i am getting error and error is below.</p>
  
  <pre><code>02-18 11:49:58.266: E/AndroidRuntime(8276): FATAL EXCEPTION: main
  02-18 11:49:58.266: E/AndroidRuntime(8276): java.lang.ClassCastException: java.net.URL cannot be cast to java.lang.String
  02-18 11:49:58.266: E/AndroidRuntime(8276):     at iqual.fidol_final.ServerFileList$1.onItemClick(ServerFileList.java:72)
  02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.widget.AdapterView.performItemClick(AdapterView.java:295)
  02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.widget.AbsListView.performItemClick(AbsListView.java:1073)
  02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2577)
  02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.widget.AbsListView$1.run(AbsListView.java:3302)
  02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.os.Handler.handleCallback(Handler.java:605)
  02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.os.Handler.dispatchMessage(Handler.java:92)
  02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.os.Looper.loop(Looper.java:154)
  02-18 11:49:58.266: E/AndroidRuntime(8276):     at android.app.ActivityThread.main(ActivityThread.java:4624)
  02-18 11:49:58.266: E/AndroidRuntime(8276):     at java.lang.reflect.Method.invokeNative(Native Method)
  02-18 11:49:58.266: E/AndroidRuntime(8276):     at java.lang.reflect.Method.invoke(Method.java:511)
  02-18 11:49:58.266: E/AndroidRuntime(8276):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
  02-18 11:49:58.266: E/AndroidRuntime(8276):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
  02-18 11:49:58.266: E/AndroidRuntime(8276):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Thank you in advance.  </p>