Android two ways to play video

Source: Internet
Author: User

layout profile for main mainactivity

This is activity_main.xml. Main appearance two buttons, button one through MediaPlayer Play, button two through Videoview play

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"     xmlns: tools= "Http://schemas.android.com/tools"     android:layout_width= "Match_parent"      android:layout_height= "match_parent"    android:orientation= "vertical"      tools:context= "Com.shao.MainActivity"  >    <Button         android:id= "@+id/btnsurfaceview"          android:layout_width= "Fill_parent"         android:layout_ height= "Wrap_content"         android:text= "Start SurfaceView"  />    <button        android:id= "@+id/ Btnvideoview "        android:layout_width=" Fill_parent "         android:layout_height= "Wrap_content"         android:text= "Start  videoview " /></LinearLayout>

main activity. For controlling two buttons, setting the layout

package com.shao;import android.app.activity;import android.content.intent;import  android.os.bundle;import android.view.view;import android.widget.button;public class  mainactivity extends activity {public button btnsurfaceview;public button  Btnvideoview, @Overrideprotected  void oncreate (bundle savedinstancestate)  {super.oncreate ( savedinstancestate) Setcontentview (r.layout.activity_main);this.btnsurfaceview =  (Button)   Findviewbyid (R.id.btnsurfaceview);this.btnvideoview =  (Button)  findviewbyid (R.id.btnVideoView); This.btnSurfaceView.setOnClickListener (New view.onclicklistener ()  {public void onclick ( VIEW&NBSP;V)  {intent intent = new intent (mainactivity.this,  Surfaceviewactivity.class); startactivity (intent);}); This.btnVideoView.setOnClickListener (New view.onclicklistener ()  {public void onclick (view&NBSP;V)  {intent intent = new intent (Mainactivity.this, videoviewactivity.class); StartActivity (intent);}});}}



1, through the MediaPlayer

package com.shao;import android.app.activity;import android.media.mediaplayer;import  android.os.bundle;import android.view.surfaceholder;import android.view.surfaceholder.callback; Import android.view.surfaceview;public class surfaceviewactivity extends activity  {private SurfaceView surfaceView;private SurfaceHolder surfaceHolder;private  Callback surfaceholdercallbac = new callback ()  {private MediaPlayer  Mediaplay, @Overridepublic  void surfacedestroyed (Surfaceholder holder)  {mediaplay.release () ;} @Overridepublic  void surfacecreated (Surfaceholder holder)  {mediaPlay =  Mediaplayer.create (Surfaceviewactivity.this, r.raw.jikexueyuan); Try {mediaplay.prepare ();}  catch (exception e)  {e.printstacktrace ();} Mediaplay.setdisplay (Surfaceholder); Mediaplay.start ();} @Overridepublic  void surfacechanged (SuRfaceholder holder, int format, int width, int height)  {}};@ Overrideprotected void oncreate (bundle savedinstancestate)  {super.oncreate ( Savedinstancestate); Surfaceview = new surfaceview (this);surfaceholder =  Surfaceview.getholder (); Surfaceholder.addcallback (SURFACEHOLDERCALLBAC); Setcontentview (SurfaceView);}}

The instance callback attribute is required and the method inside is overridden. The main content is still inside the OnCreate, Addcallback joined the SURFACEHOLDERCALLBAC.

2, through the Videoview

Package Com.shao;import Android.app.activity;import Android.os.bundle;import android.widget.mediacontroller;import Android.widget.videoview;public class Videoviewactivity extends Activity {public videoview videoview;protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); videoview = new Videoview (this); Videoview.setvideopath ("/storage/sdcard1/jikexueyuan.mp4"); Videoview.setmediacontroller (New MediaController ( this); Videoview.start (); Setcontentview (Videoview);}}

Nothing to pay attention to, is to put this videoview set Setcontentview inside. Then called a setmediacontroller, which is a system comes with the playback control component.

Android two ways to play video

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.