Tải bản đầy đủ (.doc) (3 trang)

Lập trình Android: ứng dụng xem Video pot

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (105.7 KB, 3 trang )

Trung tâm Tin học – ĐH KHTN
Ứng dụng xem Video
Tring bài viết này, mình sẽ dùng các công cụ có sẵn của Android để xây dựng 1 ứng
dụng đơn giản xem video (file MP4) trực tiếp ở 1 đường link trên mạng
1/ Tạo Project :
Project name: VideoDemo
Build Target: Android 2.3.3
Application name: VideoDemo
Package name: com.dac.VideoDemo
Create Activity: MainActivity
2/ Trong file giao diện main.xml ta thiết kế:
<?xml version="1.0" encoding="utf-8"?>
<! This file is /res/layout/main.xml >
<LinearLayout
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android=" /> <VideoView android:id="@+id/videoView"
android:layout_width="190px" android:layout_height="wrap_content"
/>
</LinearLayout>
3/ Trong file Java chính (MainActivity.java) ta viết code xử lý như sau:
package com.androidbook.videodemo;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.MediaController;
import android.widget.VideoView;
Lập trình Android – Page 1
Trung tâm Tin học – ĐH KHTN
public class MainActivity extends Activity {
/** Called when the activity is first created. */


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
VideoView videoView = (VideoView)this.findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
videoView.setVideoURI(Uri.parse(
" />p4"));
videoView.requestFocus();
videoView.start();
}
}
Để truy cập được Internet ta code trong file AndroidManifest.xml như sau:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=" /> package="com.androidbook.videodemo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
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>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

Và khi debug ứng dụng ta thấy đoạn video bắt đầu chạy:
Lập trình Android – Page 2
Trung tâm Tin học – ĐH KHTN
Lập trình Android – Page 3

×