0

I am getting all audio file list from server to list-view by following code.

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

And the output is

enter image description here

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.

I have referred below link for the reference.

How to play Audio file Mp3 from the server

how to play audio file from server in android

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.

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.

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)
Community
  • 1
  • 1
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84

3 Answers3

0

Why don't you first prepare the proper URL required to be played by converting your URL object to String?

Something like change this: playSong(urlAudio + myList.get(position));

to

playSong(urlAudio.toString() + myList.get(position));

Keya
  • 859
  • 3
  • 9
  • 17
  • Can you please post the url string you are passing to playSong()? Thanks! – Keya Feb 05 '14 at 07:27
  • It's http://i-qualtech.com/uploadaudio/....mp3, you can check above for mp3 file name. – InnocentKiller Feb 05 '14 at 07:30
  • What is myList.get(position) is returning in the call for: playSong(urlAudio + myList.get(position)); ? – Keya Feb 05 '14 at 07:50
  • It will return position of list-view item – InnocentKiller Feb 05 '14 at 07:56
  • I know what it returns technically. I want to know what is the value you are getting while making that call. Have you tried debugging the app, stopping at the 'playSong' to check what exactly is getting passed? You are concatenating 2 strings. One is clearly URL which can not be added 'as is' to a string. It needs to be converted to string by using toString method. The other part is returned by a List. Can you please break down the 2 things, store in local variables, debug and see what are the values getting populated and if they are correct. Thanks! – Keya Feb 05 '14 at 08:04
  • Okay, Thank you for answering, I solved the error by using code which is provided by @mohammad rababah but i am not able to play song. Can you check above code and tell me what is the mistake. – InnocentKiller Feb 05 '14 at 08:09
0

I don't know if this code help you or not

but try it:

before oncreate.

Uri uri;

add to your code

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

add to 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());
}
Mohammad Rababah
  • 1,730
  • 4
  • 17
  • 32
0

I have managed to solve this problem, Just added below piece of code to my onItemClicklistner and it is working fine now,

String url = null;
Object o = myList.get(position);
url = o.toString().replace(" ", "%20").trim();
play = (PlaySongAsy) new PlaySongAsy(url).execute();
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84