Android uses intent to play a local video

Source: Internet
Author: User

There are three video playback methods for Android:

1. Use intent playback. Call the locally installed player and select a player for playback.

Ii. Use VideoView for playback (VideoView is actually an encapsulation of MediaPlayer, which is easy to use but lacks flexibility ).

3. Use MediaPlayer for playback (using MediaPlayer objects for video playback provides maximum flexibility for controlling the playback itself ).

This article only explains how to use intent to play a video. The specific mechanism used to process the video playing is also MediaPlayer. The rest of the playback will be discussed in later articles.


Source code:

Layout file activity_main:

     
  
 

Code file:

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;/*** use the intent to trigger a built-in media player to play a local video. **/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);}/*** click the button and select a Video Player installed in the system to play the video. * // @ Overridepublic void onClick (View v) {/*** use Intent. ACTION_VIEW constants construct an activity and pass in the URI and MIME Type of the object 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 );}}


Before running the program, you can set a video file named 1.mp4 in the root directory of your sdks.


Source code download:

Click to download source code

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.