Use Videoview to play video in Android

Source: Internet
Author: User

First, the layout file:

  1  <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " 2  xmlns:tools=" Http://schemas.android.com/tools " 3  android:layout_width=" match_parent " 4  android:layout_height= "Match_parent"  5  android:orientation= "vertical"  6  tools:context= ". Mainactivity "> 7   8  <videoview   9  android:id=" @+id/video "10  android:layout_width=" match_parent "11  android:layou t_height= "Match_parent"/>12  13  </ Linearlayout> 

Then use the Videoview in mainactivity:

1 ImportJava.io.File;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 Importandroid.os.Environment;6 ImportAndroid.widget.MediaController;7 ImportAndroid.widget.Toast;8 ImportAndroid.widget.VideoView;9 Ten  Public classMainactivityextendsActivity { One Videoview Videoview; A Mediacontroller Mcontroller; -  - @Override the     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -Videoview =(Videoview) Findviewbyid (r.id.video); +Mcontroller =NewMediacontroller ( This); -File video =NewFile (Environment.getexternalstoragedirectory () + "/xiexienideai.mp4");//get the SD card video storage path on your phone +         if(Video.exists ()) { A Videoview.setvideopath (Video.getabsolutepath ()); atVideoview.setmediacontroller (Mcontroller);//Set Mcontroller Association Videoview - Mcontroller.setmediaplayer (videoview); -Videoview.requestfocus ();//Videoview Get Focus -}Else{ -Toast.maketext ( This, "NoNoNo", Toast.length_long). Show (); -         } in     } -  to}

Due to the use of content acquisition on the SD card, you need to add permissions in the configuration file:

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

Use Videoview to play video in Android

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.