Simple video player for Android

Source: Internet
Author: User
Public class videoactivity extends activity {
Private Static final string tag = "videoactivity ";
Private edittext filenametext;
Private surfaceview;
Private mediaplayer;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Buttonclicklistener listener = new buttonclicklistener ();
Imagebutton playbutton = (imagebutton) This. findviewbyid (R. Id. Play );
Playbutton. setonclicklistener (listener );
Imagebutton pausebutton = (imagebutton) This. findviewbyid (R. Id. Pause );
Pausebutton. setonclicklistener (listener );
Imagebutton resetbutton = (imagebutton) This. findviewbyid (R. Id. Reset );
Resetbutton. setonclicklistener (listener );
Imagebutton stopbutton = (imagebutton) This. findviewbyid (R. Id. Stop );
Stopbutton. setonclicklistener (listener );
// In this case, you need to set the callback function to inherit the callback class to implement three methods.
Surfaceview. getholder (). addcallback (New mycallback ());
Filenametext = (edittext) This. findviewbyid (R. Id. filename );
Surfaceview = (surfaceview) findviewbyid (R. Id. surfaceview );
Surfaceview. getholder (). settype (surfaceholder. surface_type_push_buffers );
// Set the size.
Surfaceview. getholder (). setfixedsize (176,144 );
Mediaplayer = new mediaplayer ();
}
Private class mycallback implements Android. View. surfaceholder. Callback {
@ Override
Public void surfacechanged (surfaceholder holder, int format, int width,
Int height ){
// Todo auto-generated method stub
}
@ Override
Public void surfacecreated (surfaceholder holder ){
// Todo auto-generated method stub
}
@ Override
Public void surfacedestroyed (surfaceholder holder ){
// Todo auto-generated method stub
}
}
// When a call arrives, the playback service must be stopped.
@ Override
Protected void onpause (){
If (mediaplayer. isplaying ())
Mediaplayer. Pause ();
Super. onpause ();
}
@ Override
Protected void onresume (){
If (mediaplayer. isplaying ())
Mediaplayer. Pause ();
Super. onresume ();
}
@ Override
Protected void ondestroy (){
If (mediaplayer. isplaying ())
Mediaplayer. Stop ();
// Note that this part is released
Mediaplayer. Release ();
Super. onstop ();
}
Private class buttonclicklistener implements view. onclicklistener {
@ Override
Public void onclick (view v ){
Try {
String filename = filenametext. gettext (). tostring ();
Switch (V. GETID ()){
Case R. Id. Play:
// Obtain the key code of the file name
String filenames = filenametext. gettext (). tostring ();
// Step 1: reset the object
Mediaplayer. Reset ();
// Key code for setting the stream type
Mediaplayer. setaudiostreamtype (audiomanager. stream_music );
// Set the data source
Mediaplayer. setdatasource ("/sdcard/" + filenames); // you can specify the video to be played.
// Output the video to the key code of the display control.
Mediaplayer. setdisplay (surfaceview. getholder ());
// Buffer before playback
Mediaplayer. Prepare ();
Mediaplayer. Start ();
Break;
Case R. Id. Pause:
If (mediaplayer. isplaying ()){
Mediaplayer. Pause ();
} Else {
Mediaplayer. Start ();
}
Break;
Case R. Id. Reset:
If (! Mediaplayer. isplaying ()){
Mediaplayer. Start ();
}
// Jump back to the starting position and play again
Mediaplayer. seekto (0 );
Break;
Case R. Id. Stop:
If (mediaplayer. isplaying ()){
Mediaplayer. Stop ();
}
Break;
}
} Catch (exception e ){
Log. E (TAG, E. tostring ());
}
}
}
}
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.