Question

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.

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://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<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);
        }
    }

So to do this i have imported ivy 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.

I have declared

    Uri uri;
    URL urlAudio;
    ListView mListView;
    PlaySongAsy play;
    MediaPlayer mp = new MediaPlayer();
    private List<String> myList = new ArrayList<String>();

mListView = (ListView) findViewById(R.id.listAudio);

and then

mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                play = (PlaySongAsy) new PlaySongAsy(myList.get(position)
                        .replace(" ", "%20").trim()).execute();
            }
        });

then here is my PlaySongAsy class

class PlaySongAsy extends AsyncTask<String, Void, Boolean> {

        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();
        }
    }

and at last Play method.

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());
        }
    }

while trying this i am getting error and error is below.

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)

Thank you in advance.

Was it helpful?

Solution

Just to be sure can we do something like this

mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {

      String url = null;
      Object o = myList.get(position);

      url = o.toString().replace(" ", "%20").trim();      
      play = (PlaySongAsy) new PlaySongAsy(url).execute();
    }
});

OTHER TIPS

You can convert you list of songs in php web service to JSON string. Than return that JSON string from web service and parse it back to list of some object and fill the addapter with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top