Video Playing with Android* Media Player

One important aspect about Android application development is that the system provides easy ways for applications to invoke features provided by other applications conveniently.  This capability of sharing features among applications helps Android developers to accelerate the time to market of their applications greatly.

For example, my restaurant menu application needs to play a video to show the “Today’s Special”. Instead of writing a full-blown video player, I will just invoke a media application installed on the device to play the video. The following screenshots show the flows of the described scenario – the user presses the “play” button to see “Today’s Special”, a dialog shows up and allows the user to choose an media application installed on the device for video playing, the user chooses “Video player” application, the video starts playing in “Video player” immediately.

Figure 1 Playing Video with another Media Application

Figure 1 Playing Video with another Media Application

Android Intent System

This capability of sharing features among applications is handled by the Android Intent mechanism. Each Android application consists of one or more screens, which are represented as Android Activities internally. An application declares the Android Activities (UI screens) that it supports in the Android manifest file. If an Activity (screen) is available for other applications, such as a video playing screen in the case of video player application, Intent Filter for the Activity will be defined in the Android manifest file.

At minimum, the Intent Filter specifies categories, actions, and the types of data that it supports for other applications to invoke. The Android system uses these information to identify which activity or screen to start when it receives the invocation request from one application. When an application wants to use a feature provided by another application, the caller application will create an intent with the action and data type specified in the Intent Filter of the callee application, and provides the intent to the Android system. If the action and data matches one of the Intent Filters registered with the system, the system will start the application. This process of identifying activity with matching intent filter is called “Intent Resolution”.
The following is sample activity and intent filter declarations from the stock Android gallery application. The activity that provides the video playing capability is “MovieActivity”. The “MovieActivity” defines three Intent Filters:

  • All three intent filters support “VIEW” intent action. We will see how to use the VIEW action to invoke the application later.
  • All three intent filters support “DEFAULT” and “BROWSABLE” categories. “DEFAULT” is required for the activity to be invokable from other applications. “BROWSABLE” is required only if the activity is invokable from browser.
  • Each intent filter also defines the URI scheme and the types of data that it supports. For example, the first one support “rtsp” (real time streaming scheme), and the second one supports various media format (MIME) from multiple URI scheme (http, https, content, file).
<activity android:configChanges="keyboardHidden|orientation|screenSize" android:label="@string/movie_view_label" android:name="com.android.gallery3d.app.MovieActivity">
<intent-filter>
       <action android:name="android.intent.action.VIEW"/>
      <category android:name="android.intent.category.DEFAULT"/>
      <category android:name="android.intent.category.BROWSABLE"/>
      <data android:scheme="rtsp"/>
</intent-filter>
<intent-filter>
      <action android:name="android.intent.action.VIEW"/>
      <category android:name="android.intent.category.DEFAULT"/>
      <category android:name="android.intent.category.BROWSABLE"/>
      <data android:scheme="http"/>
      <data android:scheme="https"/>
      <data android:scheme="content"/>
      <data android:scheme="file"/>
      <data android:mimeType="video/mpeg4"/>
      <data android:mimeType="video/mp4"/>
      <data android:mimeType="video/3gp"/>
      <data android:mimeType="video/3gpp"/>
      <data android:mimeType="video/3gpp2"/>
      <data android:mimeType="video/webm"/>
      <data android:mimeType="video/avi"/>
      <data android:mimeType="application/sdp"/>
</intent-filter>
<intent-filter> !-- HTTP live support -->;
<action android:name="android.intent.action.VIEW"/>
      <category android:name="android.intent.category.DEFAULT"/>
      <category android:name="android.intent.category.BROWSABLE"/>
      <data android:scheme="http"/>
      <data android:scheme="https"/>
      <data android:mimeType="audio/x-mpegurl"/>
      <data android:mimeType="audio/mpegurl"/>
      <data android:mimeType="application/vnd.apple.mpegurl"/>
      <data android:mimeType="application/x-mpegurl"/>
</intent-filter>
</activity>

Code sample to play video with Android media application

The following shows how to invoke Android media application from one application with intent and required action and data types. The caller application creates an intent with the action “ACTION_VIEW”, sets the data with the URI (file://) and MIME type (video/mp4) of the video file, and calls “startActivity” with the created intent to start the video playing. Please note, the sample code reads the video file from local storage.

/**
* This method is called when the user clicks the button to play the toady's 
* special video
*/
public void playVideo(View view) {
    File videoFile = new File (
        videoPath+"/today_special.mp4");
    if (videoFile.exists()) {
        Uri fileUri = Uri.fromFile(videoFile);
	  Intent intent = new Intent();
	  intent.setAction(Intent.ACTION_VIEW);
	  intent.setDataAndType(fileUri,                
            URLConnection.guessContentTypeFromName(fileUri.toString()));
	  startActivity(intent);
    } else {
        Toast.makeText(this, "Video file does not exist", 
            Toast.LENGTH_LONG).show();
    }
}

By default, the Android system will show a dialog (Intent Chooser) to a user if the system finds more than one activity supporting the same intent filter (as shown in figure 1). This gives the user an opportunity to play the video with their preferred video player. From the dialog, the user can specify the preferred video player by clicking “Always” so that the same dialog is not shown again on the next video playing.  

About the Author

Mei-Lin Hsieh is a software engineer with 15 years of experience in mobile development at Intel and other companies. She is currently in the Software Solutions Group working on scale enabling projects for Android applications with tablets and phones.

Notices 
INFORMATION IN THIS DOCUMENT IS PROVIDED IN CONNECTION WITH INTEL PRODUCTS. 
NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, TO ANY INTELLECTUAL 
PROPERTY RIGHTS IS GRANTED BY THIS DOCUMENT. EXCEPT AS PROVIDED IN INTEL'S 
TERMS AND CONDITIONS OF SALE FOR SUCH PRODUCTS, INTEL ASSUMES NO LIABILITY 
WHATSOEVER AND INTEL DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY, RELATING TO 
SALE AND/OR USE OF INTEL PRODUCTS INCLUDING LIABILITY OR WARRANTIES RELATING 
TO FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT. 
 
UNLESS OTHERWISE AGREED IN WRITING BY INTEL, THE INTEL PRODUCTS ARE NOT 
DESIGNED NOR INTENDED FOR ANY APPLICATION IN WHICH THE FAILURE OF THE INTEL 
PRODUCT COULD CREATE A SITUATION WHERE PERSONAL INJURY OR DEATH MAY OCCUR. 
 
Intel may make changes to specifications and product descriptions at any time, without notice. Designers must not rely on the absence or characteristics of any features or instructions marked "reserved" or "undefined." Intel reserves these for future definition and shall have no responsibility whatsoever for conflicts or incompatibilities arising from future changes to them. The information here is subject to change without notice. Do not finalize a design with this information. 
 
The products described in this document may contain design defects or errors known as errata which may cause the product to deviate from published specifications. Current characterized errata are available on request. 
 
Contact your local Intel sales office or your distributor to obtain the latest specifications and before placing your product order. 
 
Copies of documents which have an order number and are referenced in this document, or other Intel literature, may be obtained by calling 1-800-548-4725, or go to: 
http://www.intel.com/design/literature.htm 
 
Software and workloads used in performance tests may have been optimized for performance only on Intel microprocessors. Performance tests, such as SYSmark* and 
MobileMark*, are measured using specific computer systems, components, software, operations, and functions. Any change to any of those factors may cause the results to vary. 
You should consult other information and performance tests to assist you in fully evaluating your contemplated purchases, including the performance of that product when combined with other products.

       **This sample source code is released under the Intel Sample Source Code License Agreement