Android uses intent to play local video

Source: Internet
Author: User

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

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.