Media in Android Using Web & SD card


Android's open platform provides API capable of playing and recording a wide range of image, audio, and video formats, both locally and streamed content under the android.media package.
The class android.media.MediaPlayer is the heart of the android.media package. The content of the android.media.MediaPlayer is coming from the sources such as:
  • Web
  • .apk file
    We can play content that is packaged. Our media content can be packaged as a resource or as an asset.
  • SD card
The MediaPlayer can handle the following different formats:
  • H.264 format, m4v container
  • 3GPP format, 3gp container
  • MPEG-4 format, mp4 container
  • PCM/WAVE format, wav container
1. VideoView

In this example, we'll learn how to playback a video either from a web server or from a SD card.
Android provides a specialized view control, android.widget.VideoView that encapsulates creating and initializing the MediaPlayer.
Let's look at the our files
layout file, /res/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<VideoView android:layout_width="match_parent"
    android:id="@+id/videoView1"  
    android:layout_height="wrap_content">
    </VideoView>
</LinearLayout>

manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.murali.media"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
   
<uses-permission android:name="android.permission.INTERNET">
    </uses-permission>

     <application android:icon="@drawable/icon"
                  android:label="@string/app_name">
        <activity android:name=".MediaActivity"
                  android:label="@string/app_name">
          
            <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
       
       </activity>
    </application>
</manifest>


and finally, our Java code,

package com.murali.media;



import android.app.Activity;

import android.net.Uri;

import android.os.Bundle;

import android.widget.MediaController;

import android.widget.VideoView;



public class MediaActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        VideoView videoView = (VideoView)this.findViewById(R.id.videoView1);

        MediaController mc = new MediaController(this);

        videoView.setMediaController(mc);

      

       // (1) Web

       videoView.setVideoURI(Uri.parse("http://www.bogotobogo.com/Video/sample.3gp"));

       //"http://www.google.com/Video/sample.3gp"));

       //"http://www.google.com/Video/sample.mp4"));

       //"http://www.google.com/Video/sample.mov"));

      

       /* (2) SD card */

       //videoView.setVideoPath("/sdcard/sample.3gp");

       //videoView.setVideoPath("/sdcard/sample.mp4");

       //videoView.setVideoPath("/sdcard/sample.mov");

       //videoView.setVideoURI(Uri.parse("file:///sdcard/sample.mov"));

      

       videoView.requestFocus();

       videoView.start();

   

    }

}


Here we have 3 video files:


To play video, we need to create a VideoView widget and set that as the content of the UI. 

   this.setContentView(R.layout.main);

VideoView videoView = (VideoView)this.findViewById(R.id.videoView1);
  


When the application runs, we'll see the controls for about three seconds, and then they disappear. We get them back by clicking anywhere within the video frame. 

All of the playback functionality is hidden behind the VideoView class. All we're doing here is feeding the video contents to the video player.

In this example, we're using a VideoView component to display the video content. However, instead of creating our own button controls, we create a MediaController that provides the buttons for us.

 
      MediaController mc = new MediaController(this);

      videoView.setMediaController(mc);

 
Actually, calling setMediaController() enables several controls such as play, pause etc.

While MediaPlayer has a setDataSource() method, VideoView does not. VideoView uses the setVideoPath() or setVideoURI() methods.

// (1) Web
   videoView.setVideoURI(Uri.parse("http://www.bogotobogo.com/Video/sample.3gp"));
       //"http://www.google.com/Video/sample.3gp"));
       //"http://www.google.com/Video/sample.mp4"));
       //"http://www.google.com/Video/sample.mov"));
      
/* (2) SD card */
       //videoView.setVideoPath("/sdcard/sample.3gp");
       //videoView.setVideoPath("/sdcard/sample.mp4");
       //videoView.setVideoPath("/sdcard/sample.mov");
       //videoView.setVideoURI(Uri.parse("file:///sdcard/sample.mov"));

 
Note that we need to set permission for the Web case use.        


<uses-permission android:name="android.permission.INTERNET">
    </uses-permission>

Files used in this VideoView example, Video.zip

In the next section, we'll learn how to put the video files into the SD card.

2.SD Card


SD cards are one of the sources of content for the MediaPlayer. They are used in Android phones for storing user data, usually media content such as pictures, audio, and video. 


Let's bring up File Explorer. 


Start the emulator and wait the emulator initializes. Then,


Window > Show View > Other.



 

 

Select File Explorer, then click OK.


To push a file onto the SD card, select the sdcard folder in the File Explorer and choose the button with the right-facing arrow which is at the top-right corner. This launches a dialog box that lets you select a file. Select the file that we want to upload to the SD card.


Now we have put all files into the SD card.


One more thing. The directory called DCIM is the Digital Camera Images directory. It's an industry standard to put a DCIM directory within the root directory of an SD card that's used for digital images.


If you are creating an application whose target is Android 1.6 or newer and you want to be able to write to the SD card, you need to add the permission to write:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">

      </uses-permission>


Comments

Popular posts from this blog

How To Run Compatibility Test Suite (CTS) ?

NFC : Reading a MiFare Classic 1K from Android Devices

Dalvik Virtual machine VS Java Virtual Machine: