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