from: http://www.cnblogs.com/rxwen/archive/2009/11/30/1713031.html
In this post, I'll show some basic operations in android with a simple video player. The demo shows how to:
Use explicit intent
List video files with ListView
, ContentProvider & MediaProvider
Retrieve data from another Activity with startActivityForResult
Play video file with VideoView
Basically,
the demo is composed of two activities. The first activity (Main) is a
video player with a button on it. When you click the button, the second
activity(VideoSelector) shows and lists video files available on the
device. Then, after you pick a video from the list, you return to Main
activiry and it starts playing the video you selected.
1. Use explicit intent
On
android, an intent can be implicit or explicit. An explicit intent
clearly designates the component to start. Because explicit intent uses
the name of target component, it's usually used inside an application.
On contrast, implicit intent specifies desired action, and let the
system to pick the best match component.
In our demo, we want to use
the VideoSelector to select a video file, rather than the system
built-in media selector, so we use explicit intent to start activity.
To create explicit intent, we just need to pass the designated
component class name to intent, as shown below.
Intent i = new Intent();
i.setClass(v.getContext(), VideoSelector.class);
startActivityForResult(i, 0);
The use of startActivityForResult will be explained later.
2. List video files
Android
provides a ListActivity class to list data provided by a
ContentProvider. We can take advantage of it to list video files
available on current device rather than start from scratch. To use it,
we simply make it the base class for our activity, meanwhile, add a
ListView with id "@android:id/list" in the activity's layout. And the
final thing we need to do is set the datasource for the list with
setListAdapter method.
Android also provides a MediaProvider that can list all video files. It's the default provider when we use ContentResolver
to query MediaStore.Video.Media.
3. Get data from another activity & play video
After
we select a video from the list, information of the video such as its
path should be passed to the main activity for playing. The recommended
way is passing it via intent object. Previously, we start the
VideoSelector with startActivityForResult in Main. Being started this
way, when the VideoSelector is finished, the onActivityResult method of
Main will be fired. So, we need to store the selected video's path in
the intent to be passed to Main. Then retrieve it in Main. As shown
below:
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{ // in VideoSelector.java
String filePath = mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.DATA));
mCursor.moveToPosition(position);
Intent result = new Intent();
result.putExtra(FILE_PATH, filePath);
setResult(RESULT_OK, result);
finish();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// in Main.java
super.onActivityResult(requestCode, resultCode, data);
if (RESULT_OK == resultCode) {
Bundle bd = data.getExtras();
String path = bd.getString(VideoSelector.FILE_PATH);
m_vvPlayer.setVideoPath(path);
m_vvPlayer.start();
}
}
To download
full source code for this demo, you can download them from this page:
http://code.google.com/p/rxwen-blog-stuff/source/browse/#svn/trunk/android/mplayer
or with following svn command:
svn co https://rxwen-blog-stuff.googlecode.com/svn/trunk/android/mplayer