Search code examples
androidandroid-asynctaskandroid-mediaplayer

Playing media using asynctask(android)


I am trying to play media at different intervals. I have a function that plays the media void playAudio() and when ever a media file is played a asynctask is executed to start calculating the time for the next media.

The intervals are calculated in the doInBackground() method of my asynctask. When the interval is up the onPostExecute function calls that playAudio() function. So after the first media file is played the app crashes. Help please!

public void startNextFile() {

    if (mCompleted < nums.size()) {
        int a=Integer.parseInt(nums.get(c++));



        currentTime=System.currentTimeMillis();
        mp[mCompleted] = MediaPlayer.create(this, songIds[a]);


        mp[mCompleted].start();
        ArrayList<String> payload=new ArrayList<String>(0);

        payload.add(++mCompleted+"");
        payload.add(differences.get(mCompleted-1));
        payload.add(currentTime+"");

        AsyncTask thread=new AsyncTask<ArrayList<String>,Void,Boolean>(){

            @Override
            protected Boolean doInBackground(ArrayList<String>[] strings) {
                //int idx= Integer.parseInt(strings[0].toString());
                long diff=Long.parseLong(strings[1].toString());
                long current_time=Long.parseLong(strings[2].toString());
                boolean isTime=false;
                while(!isTime)
                {
                    if(System.currentTimeMillis()-current_time==diff)
                    {
                        isTime=true;
                    }
                }
                return isTime;
            }

            @Override
            protected void onPostExecute(Boolean aBoolean) {
                super.onPostExecute(aBoolean);
                startNextFile();

            }
        };

        thread.execute(payload);



    }
    else
    {
        mCompleted=0;
    }
}

Edit: So, after checking the logcat it seems there is this error: "Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.util.ArrayList[] at com.example.cracked.pianist.Player$2.doInBackground(Player.java:208)"

208 is the line where Async task starts

Updated code just to check the doInBackground function

public void startNextFile() {

    if (mCompleted < nums.size()) {
        int a=Integer.parseInt(nums.get(c++));



        currentTime=System.currentTimeMillis();
        mp[mCompleted] = MediaPlayer.create(this, songIds[a]);


        mp[mCompleted].start();
        ArrayList<String> payload=new ArrayList<>(0);
        //Toast.makeText(Player.this,"starting thread",Toast.LENGTH_SHORT).show();
        payload.add(++mCompleted+"");
        payload.add(differences.get(mCompleted-1)+"");
        payload.add(currentTime+"");

        AsyncTask thread=new AsyncTask<ArrayList<String>,Void,Integer>(){

            @Override
            protected Integer doInBackground(ArrayList<String>[] arrayLists) {
                //int idx= Integer.parseInt(arrayLists[0].get(0));
                //long diff=Long.parseLong(arrayLists[0].get(1));
                //long current_time=Long.parseLong(arrayLists[0].get(2));
                //long current_time=System.currentTimeMillis();

                int count=Integer.parseInt(arrayLists[0].get(0));
                //boolean isTime=false;
                //Log.d("starting","loop starting");
                /*while(!isTime)
                {
                    if(System.currentTimeMillis()-current_time==diff)
                    {
                        isTime=true;
                    }
                }*/
                return count;
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }


            @Override
            protected void onPostExecute(Integer integer) {
                super.onPostExecute(integer);
                Log.d("error","size is: "+integer);
                Toast.makeText(Player.this,"Thread done "+integer,Toast.LENGTH_SHORT).show();

            }
        };

        thread.execute(payload);



    }
    else
    {
        mCompleted=0;
    }
}

Solution

  • You are passing ArrayList instead of String value for parseLong() method. That's why your getting class cast exception.

    Change this

    long diff=Long.parseLong(strings[1].toString());
    long current_time=Long.parseLong(strings[2].toString());
    

    to

    long diff=Long.parseLong(strings[0].get(0));
    long current_time=Long.parseLong(strings[0].get(1));
    

    and if you need any other help then let me know.