Android VideoPlayer 1

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
在 Unity 中,你可以使用 VideoPlayer 组件来播放 MP4 视频。要播放 MP4 视频,可以按照以下步骤进行: 1. 创建一个空的 GameObject,并将 VideoPlayer 组件添加到该 GameObject 上。 2. 在代码中获取该 GameObject,并获取 VideoPlayer 组件的引用,例如: ```csharp using UnityEngine; using UnityEngine.Video; public class VideoPlayerLoader : MonoBehaviour { public GameObject videoPlayerObject; private VideoPlayer videoPlayer; void Start() { videoPlayer = videoPlayerObject.GetComponent<VideoPlayer>(); } } ``` 这个代码创建了一个名为 VideoPlayerLoader 的脚本,并在其中获取了名为 videoPlayerObject 的 GameObject 的 VideoPlayer 组件的引用。 3. 加载 MP4 视频文件。Unity 支持从本地文件、URL、网络流等多种方式加载视频文件。下面是一个从本地文件夹加载 MP4 视频的例子: ```csharp void LoadVideo(string path) { videoPlayer.source = VideoSource.Url; videoPlayer.url = "file://" + path; videoPlayer.Prepare(); } ``` 这个代码定义了一个名为 LoadVideo 的方法,用于加载 MP4 视频文件。该方法将 VideoPlayer 组件的 source 属性设置为 Url,将 url 属性设置为 MP4 视频文件的路径,并调用 Prepare() 方法准备 MP4 视频文件的播放。 需要注意的是,在安卓平台上,需要在 AndroidManifest.xml 文件中添加相关权限,例如: ```xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ``` 这个权限用于读取外部存储器上的视频文件。 4. 播放 MP4 视频。MP4 视频文件加载完成后,你可以通过调用 Play() 方法开始播放 MP4 视频。例如: ```csharp void Start() { videoPlayer = videoPlayerObject.GetComponent<VideoPlayer>(); LoadVideo("path/to/video.mp4"); videoPlayer.Play(); } ``` 这个代码在 Start() 方法中调用了 LoadVideo() 方法加载 MP4 视频文件,并在加载完成后调用 Play() 方法开始播放 MP4 视频。 需要注意的是,MP4 视频的编码参数可能会影响播放效果,例如视频的分辨率、帧率、编码方式等等。如果出现播放效果不佳的情况,可以尝试调整 MP4 视频的编码参数或使用其他编码方式进行视频编码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值