I don't use TimerTask in Java. For reasons that were never 100% clear
to me, the online docs for the JDK and other sources recommend using a
Handler instead. There is even an example in code in the docs for
Handler somewhere, but as I write this, I can only find a similar one
at http://www.muktosoft.com/timer-in-android-the-better-way/, also a
good source.

But if you really want to use a TimerTask, see
http://developer.android.com/resources/articles/timed-ui-updates.html

On Jun 29, 1:17 pm, New Developer <[email protected]> wrote:
> I'm struggling to close these two timer tasks
>
> I have
>         @Override
>         public void onCompletion(MediaPlayer arg0) {
>                 try {
>                         mMediaPlayer.stop();
>                         mMediaPlayer = null;
>                         if (timer != null) {
>                                 timer.cancel();                
>                                 timer.purge();
>                                 Thread.sleep(500);
>                         }
>                         timer       = null;
>                 } catch (Exception e) {
>                 }
>         }
>
> The sleep just to give it chance to run through
> But I get a NullPoinerException  on the sleep line
>
> Thread [<12> Timer-0] (Suspended (exception NullPointerException))        
>         Timer$TimerImpl.run() line: 290
>
> this    Timer$TimerImpl  (id=830020453552)      
> currentTime     1309378473189  
> pos     0      
> task    Video2$2  (id=830013266400)    
>         cancelled       false  
>         fixedRate       false  
>         lock    Object  (id=830013265024)      
>         period  500    
>         scheduledTime   1309378473189  
>         this$0  Video2  (id=830013820608)      
>         when    1309378473690  
>
> So how does one close the timer once the video has come to and end ?
>
> Thanks in advance
>
> On Jun 29, 2011, at 2:53 PM, New Developer wrote:> To Daniel
>
> > Thanks again
> > Okay that seems to be working
>
> > Just to clarify
> > the 0 , 200  implies  start immediately  and re-occur every 200 
> > milliseconds ?
>
> > the 100 , 200 implies start in 100 milliseconds and re-occur every 200 
> > milliseconds ?
>
> > Thus there is two cycles going 100 milliseconds apart  causing it to play 
> > for 100 milliseconds and then pause for 100 milliseconds
> > so it plays (for 100 ms)  pauses for (100 ms)  
>
> > am I right ?
>
> > thanks again
>
> > On Jun 29, 2011, at 9:46 AM, Daniel Drozdzewski wrote:
>
> >> On Wed, Jun 29, 2011 at 1:37 PM, New Developer <[email protected]> wrote:
> >>> To Daniel
> >>> I have tried pause no success
> >>> can you let me know more what you were thinking  perhaps I have 
> >>> implemented it incorrectly ??
>
> >>> Thanks again
>
> >> Sure, I was thinking following (I have not implemented this myself mind):
>
> >> <code>
> >> MediaPlayer player;
> >> Timer timer = new Timer();
> >> //initialisation of the player
>
> >> timer.schedule( new TimerTask() {
> >> run() {
> >>    player.start();
> >> }
> >> }, 0, 200);
>
> >> timer.schedule( new TimerTask() {
> >> run() {
> >>    player.pause();
> >> }
> >> }, 100, 200);
>
> >> </code>
>
> >> You schedule 2 recurring tasks on a Timer: one to resume/start the
> >> video every 200 milliseconds, and the second to pause it also every
> >> 200 milliseconds, with 100 millisecond offset. This should slow the
> >> playback by the factor of 2. You can adjust the numbers to get
> >> different ratios and I would not play or pause for longer than say 500
> >> milliseconds, as it will look jerky.
>
> >> It is possible that both timers will diverge (no real-time promise),
> >> in which case you will have to do something a bit more clever than
> >> arbitrary numbers, as the paused_time / play_time ratio will shift.
>
> >> You will probably need to monitor actual track progress by using
> >> MediaPlayer.getCurrentPosition(), rather than the inaccurate wall
> >> clock.
>
> >> Daniel
>
> >>> On Jun 12, 2011, at 3:52 AM, Daniel Drozdzewski wrote:
> >> ^^^^^^^^^^^^^^^----- Internet got very slow or The Matrix is
> >> overloaded (could not resist)
>
> >> --
> >> You received this message because you are subscribed to the Google
> >> Groups "Android Developers" group.
> >> To post to this group, send email to [email protected]
> >> To unsubscribe from this group, send email to
> >> [email protected]
> >> For more options, visit this group at
> >>http://groups.google.com/group/android-developers?hl=en
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to