5-Step learning to play video with Videoview

Source: Internet
Author: User

We can imagine ImageView can display pictures, and Videoview is used to show the video
To play a video using Videoview, follow these steps
"1" defines videoview in the interface layout

<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=". Mainactivity ">                      <videoview  android:id  = "@+id/videoview"  android:layout_width  = "match_parent"  android:layout_height  = "0DP"  android:layout_weight  = "1" />     <buttonandroid:id="@+id/button"android:text="Play"  Android:layout_width="Wrap_content"android:layout_height="Wrap_content" />                                 </linearlayout>

"2" invokes the following two methods to load the specified video

Setvideopath (String path), video under load path
Setvideourl (URL URL); Loads the video corresponding to the URL.

  mVideoView.setVideoPath(Environment.getExternalStorageDirectory()+"/aa.mp4");

"3" permission

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

"4" Call
Start (), Stop (), pause () control playback
"5" In practice often combines the Mediacontroller class, which provides a friendly image control interface to control video playback;

mVideoView.setMediaController(new MediaController(MainActivity.this));
The complete program code is as follows
 Public  class mainactivity extends Activity {    PrivateVideoview Mvideoview;PrivateButton Mbutton;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        mvideoview= (Videoview) Findviewbyid (R.id.videoview);        mbutton= (Button) Findviewbyid (R.id.button); Mbutton.setonclicklistener (NewView.onclicklistener () {@Override             Public void OnClick(View v) {//Get sdcard aa.mp4 video files below                //Two ways to call//File videofile =new file ("/mut/extsdcard/dcim/camera/20150915_160202.mp4");//Mvideoview.setvideopath (Videofile.getabsolutepath ());Mvideoview.setvideopath (environment.getexternalstoragedirectory () +"/20150915_160202.mp4"); Mvideoview.setmediacontroller (NewMediacontroller (mainactivity. This));            Mvideoview.start ();    }        }); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

5-Step learning to play video with Videoview

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.