1.AndroidManifest.xml程式碼
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
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>
<activity
android:name="HelloVideoView"
android:label="@string/app_name" />
</application>
</manifest>
2.第一個UI檔的xml程式碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/PhoneMusicList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
3.第二個UI檔的xml程式碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<VideoView
android:id="@+id/videoView01"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
4.MainActivity.java程式碼
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init_phone_video_grid();
}
// 影像檔案匯入時系統會在資料庫的Video View上建立對應資料列
// 因此Android的對應類別就是MediaStore.Video
// 透過Eclipse就可以找到下面幾個欄位資料
// MediaStore. Video.Media._ID, MediaStore. Video.Media.DATA
// MediaStore. Video.Media.DISPLAY_NAME, MediaStore.Video.Media.SIZE
// MediaStore. Video.Media.DATA是音樂檔案所在路徑位置
private void init_phone_video_grid() {
System.gc();
String[] proj = { MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
musiccursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
count = musiccursor.getCount();
musiclist = (ListView) findViewById(R.id.PhoneMusicList);
musiclist.setAdapter(new MusicAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
}
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
System.gc();
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
musiccursor.moveToPosition(position);
String filename = musiccursor.getString(music_column_index);
Intent it = new Intent(MainActivity.this, HelloVideoView.class);
it.putExtra("VideoPath", filename);
startActivity(it);
}
};
public class MusicAdapter extends BaseAdapter {
private Context mContext;
public MusicAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv1 = new TextView(mContext.getApplicationContext());
TextView tv2 = new TextView(mContext.getApplicationContext());
ImageView img = new ImageView(mContext.getApplicationContext());
// 設定圖片及上下文所需要的畫面元件
img.setImageResource(R.drawable.ic_launcher);
tv1.setTextSize(12.0f);
tv2.setTextSize(12.0f);
String id = null;
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
tv1.setText(id);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
musiccursor.moveToPosition(position);
id = "Size(MB):"
+ String.format("%,5.2f", Double
.parseDouble(musiccursor
.getString(music_column_index)) / 1024);
tv2.setText(id);
// 產生兩個線性佈局物件第一個是水平方向佈局
// 左邊加入圖片右邊加入第二個線性佈局方向是垂直方向佈局
// 最後加入文字元件到第二個線性佈局內
LinearLayout layout = new LinearLayout(
mContext.getApplicationContext());
LinearLayout innerLayout = new LinearLayout(
mContext.getApplicationContext());
layout.setOrientation(LinearLayout.HORIZONTAL);
innerLayout.setOrientation(LinearLayout.VERTICAL);
innerLayout.addView(tv1);
innerLayout.addView(tv2);
layout.addView(img);
layout.addView(innerLayout);
return layout;
}
}
}
5.HelloVideoView.java程式碼
import android.app.Activity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class HelloVideoView extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myvedio);
VideoView videoView = (VideoView) findViewById(R.id.videoView01);
String p = getIntent().getStringExtra("VideoPath");
videoView.setVideoPath(p);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
videoView.requestFocus();
videoView.start();
}
}
沒有留言:
張貼留言