Android video playback and progress bar Synchronization

Source: Internet
Author: User

First, get the video playback length to set the max and videobbar of seekbar. setmax (mediaplayer. getduration (), and then obtain the video playing time in real time through the thread to update seekbar and videobbar. setprogress (mediaplayer. the getcurrentposition () method is as follows.CodeAs follows:

Videobbar = (seekbar) findviewbyid (R. Id. seekbarplayprogress );

surfaceview = (surfaceview) findviewbyid (R. id. player);
surfaceview. getholder (). addcallback (New playcallback ();
surfaceview. getholder ()
. settype (surfaceholder. surface_type_push_buffers);
surfaceview. getholder (). setfixedsize (surfaceview. getwidth (), surfaceview. getheight ();
mediaplayer = new mediaplayer ();
mediaplayer. setaudiostreamtype (audiomanager. stream_music);
mediaplayer. setdisplay (surfaceview. getholder ();
try {
mediaplayer. setdatasource ("/sdcard/movie/ _1.mp4");
mediaplayer. prepare ();
mediaplayer. start ();
videobbar. setmax (mediaplayer. getduration ();

Videothreed = new videothreed ();
Videothreed. Start ();
}
Catch (exception E)
{
Toast. maketext (this, "playing error! ", Toast. length_long). Show ();
}

 

 

// Update the UI
Handler myhandler = new handler ()
{
Public void handlemessage (Message MSG)
{
Switch (msg. What)
{
Case progress_changed:
Videobbar. setprogress (mediaplayer. getcurrentposition ());
Break;
}
}
};

// Update the video progress bar
Class videothreed extends thread
{
Public void run ()
{
While (! Thread. currentthread (). isinterrupted ())
{
If (mediaplayer. getcurrentposition () = mediaplayer. getduration ())
{
Return;
}
Message message = new message ();
Message. What = progress_changed;
Videoplayer. This. myhandler. sendmessage (Message );
Try
{
Thread. Sleep (1000 );
}
Catch (interruptedexception E)
{
Thread. currentthread (). Interrupt ();
}
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.