使用asynctask播放媒体(android)

时间:2017-12-01 09:41:18

标签: android android-asynctask android-mediaplayer

我正在尝试以不同的间隔播放媒体。 我有一个播放媒体文件void playAudio()的函数,当播放媒体文件时,执行asynctask以开始计算下一个媒体的时间。

间隔在我的asynctask的doInBackground()方法中计算。当间隔结束时,onPostExecute函数调用该playAudio()函数。 因此,在播放第一个媒体文件后,应用程序崩溃了。 求救!

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

编辑: 所以,在检查logcat后,似乎有这个错误: &#34;引起:java.lang.ClassCastException:java.lang.Object []无法强制转换为java.util.ArrayList []                                                                                在com.example.cracked.pianist.Player $ 2.doInBackground(Player.java:208)&#34;

208是Async任务启动的行

更新代码只是为了检查doInBackground函数

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

1 个答案:

答案 0 :(得分:0)

您正在为parseLong()方法传递ArrayList而不是String值。这就是为什么你的课程被抛出异常。

更改此

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

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

如果您需要任何其他帮助,请告诉我。