There are three ways to play video on Android:
First, use the intention to play, call the locally installed player, select one to play.
Second, the use of Videoview play (Videoview is in fact the MediaPlayer package, very easy to use, but lack of flexibility).
Third, use MediaPlayer play (the MediaPlayer object for video playback can provide maximum flexibility to control the playback itself).
This article only explains the use of intent to play the video, the detailed mechanism for handling playback is also MediaPlayer. The rest of the playback will be described in a later article.
Source:
Layout file Activity_main:
<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" > <button android:id= "@+id/button" android:layout_width= " Match_parent " android:layout_height=" wrap_content " android:text=" Play/></linearlayout>
Code files:
Mainactivity:
Package Com.multimediademo10videointent;import Android.app.activity;import Android.content.intent;import Android.net.uri;import Android.os.bundle;import Android.os.environment;import Android.view.View;import Android.view.view.onclicklistener;import android.widget.button;/** * Local video playback by using the intent to trigger the built-in media player.* */public class Mainactivity extends Activity implements Onclicklistener {private button button; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button = (button) Findviewbyid (R.id.button); Button.setonclicklistener (this);} /** * After clicking the button. Select the system that has the video player installed to play the video. */@Overridepublic void OnClick (View v) {/** * Constructs an activity using the Intent.action_view constant. And the URI and MIME type of the file passed through the Setdataandtype method */intent Intent = new Intent (Android.content.Intent.ACTION_VIEW); Uri data = Uri.parse (Environment.getexternalstoragedirectory (). GetPath () + "/1.mp4"), Intent.setdataandtype (data, " Video/mp4 "); StartActivity (intent);}}
What needs to be explained is. Before you perform the change procedure, you need to place a video file named 1.mp4 in the root folder of your SD card.
SOURCE Download:
Click to download source code
Android uses intent to play local video