samurai
samurai

Reputation: 101

Android TimerTask shut down when I open the app

I write a program I hope it can grab remote mp3 mediaplayer duration
and duration can run in timer.schedule, after mp3 finish, it can change to another song and next viewflipper.
this is my code:

TimerTask t4=new TimerTask() {
    @Override
    public void run() {
       int duration=0;
        url = "http://demo.akkyschool.com/mp3/"+filename[tmp];//filename[tmp] is st02_01.mp3
        mediaplayer.reset();
        try {
            mediaplayer.setDataSource(url);
            mediaplayer.getDuration();
            mediaplayer.prepare();
            duration=  mediaplayer.getDuration();

        } catch (IOException e) { }
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

                        mediaplayer.reset();

                        try {viewflipper.addView(getImageView(draw[tmp]),tmp+1,params);
                            mediaplayer.setDataSource(url);
                            mediaplayer.prepare();
                            mediaplayer.start();
                        } catch (IOException e) { }
                        tmp++;
                        viewflipper.startFlipping();
                        if(tmp==5){tmp=0;}

                    }
                });
            }
        }, duration);
    }
};

I do not know why it shut down.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nihon.aki2/com.nihon.aki2.Childstory}: java.lang.IllegalArgumentException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.IllegalArgumentException

thanks.

Upvotes: 0

Views: 60

Answers (1)

Mostafa Soliman
Mostafa Soliman

Reputation: 872

First
If you want the duration you will need to download the audio then load it to MediaPlayer

Second
set OnPreparedListener then start the audio in it and getDuration too

 mediaplayer.setOnPreparedListener(new 
    MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
             // mp.start();
                duration = mp.getDuration();
            }
        });

Upvotes: 1

Related Questions