eclipse 3.7.2
Android 4.1 SDK (範例使用Android SDK 2.2)
設計步驟如下:
1. File->New->Project
在Application Name欄位中填入videoPlay,連續按下Next鍵3次。
2.把Activity Name欄位改成videoPlayActivity。
3.選擇專案下res\layout下的activity_video_play.xml,先把TextView刪除,並在Palette選擇VideoView控制項,拖移至視窗中。
Layout XML資料如下(紅色字代表新增):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<VideoView
android:id="@+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
4. 宣告及連繫VideoView物件
package
com.example.videoplay;
import
android.os.Bundle;
import
android.app.Activity;
import
android.widget.VideoView;
public
class videoPlayActivity
extends Activity {
VideoView videoView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_play);
videoView
= (VideoView)
findViewById(R.id.videoView1);
}
}
5. 建立播放程式
package
com.example.videoplay;
import
android.os.Bundle;
import
android.app.Activity;
import
android.widget.VideoView;
public
class videoPlayActivity
extends Activity {
VideoView videoView;
String
videofile
= "file:///sdcard/"+"yourvideo.mp4";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_play);
videoView = (VideoView) findViewById(R.id.videoView1);
videoView.setVideoPath(videofile);
videoView.start();
}
}
"yourvideo.mp4"記得改成您的影片檔
6. 執行和測式,記得把影片放在SD Card。