Android VideoPlayer 1

來源:互聯網
上載者:User

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.

EXTERNAL_CONTENT_URI.

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.