android视频播放

时间:2011-11-19 08:03:09

标签: android

我是android的新手。 任何人都可以帮我如何从Sd卡显示图像或从Sd卡播放视频.. 我尝试了很多方法,但没有一个工作..

<?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" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />


<VideoView
    android:id="@+id/videoView1"
    android:layout_width="243dp"
    android:layout_height="234dp" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

here是关于VideoView的完整参考示例。 您只需在此处设置视频文件的路径。

答案 1 :(得分:0)

这会将所有歌曲从sdcard绑定到listview ....

public class VideoListActivity extends ListActivity {
    private MediaCursorAdapter mediaAdapter = null;

    private String currentFile = "";
    VideoView video;
    MediaController mediaController;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        video = (VideoView) findViewById(R.id.videoView1);
        mediaController = new MediaController(this);
        mediaController.setAnchorView(video);
        video.setMediaController(mediaController);
        video.setKeepScreenOn(true);


        Cursor cursor = getContentResolver().query(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null,
                null);

        if (null != cursor) {
            cursor.moveToFirst();

            mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem,
                    cursor);

            setListAdapter(mediaAdapter);


        }
    }

    @Override
    protected void onListItemClick(ListView list, View view, int position,
            long id) {
        super.onListItemClick(list, view, position, id);

        currentFile = (String) view.getTag();
        video.setVideoPath(currentFile);
        video.start();
        video.requestFocus();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    private class MediaCursorAdapter extends SimpleCursorAdapter {

        public MediaCursorAdapter(Context context, int layout, Cursor c) {
            super(context, layout, c, new String[] {
                    MediaStore.Video.VideoColumns.DISPLAY_NAME,
                    MediaStore.Video.VideoColumns.ARTIST,
                    MediaStore.Video.VideoColumns.DURATION }, new int[] {
                    R.id.displayname, R.id.title, R.id.duration });
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView title = (TextView) view.findViewById(R.id.title);
            TextView name = (TextView) view.findViewById(R.id.displayname);
            TextView duration = (TextView) view.findViewById(R.id.duration);

            name.setText(cursor.getString(cursor
                    .getColumnIndex(MediaStore.Video.VideoColumns.DISPLAY_NAME)));

        String str=cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.ARTIST));

        if(str.equals("<unknown>"))
            title.setText(" ");
        else
        title.setText(""
                    + cursor.getString(cursor
                            .getColumnIndex(MediaStore.Video.VideoColumns.ARTIST)));

            long durationInMs = Long.parseLong(cursor.getString(cursor
                    .getColumnIndex(MediaStore.Video.VideoColumns.DURATION)));

            double durationInMin = ((double) durationInMs / 1000.0) / 60.0;

            durationInMin = new BigDecimal(Double.toString(durationInMin))
                    .setScale(2, BigDecimal.ROUND_UP).doubleValue();

            duration.setText("" + durationInMin);

            view.setTag(cursor.getString(cursor
                    .getColumnIndex(MediaStore.Video.VideoColumns.DATA)));
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(context);
            View v = inflater.inflate(R.layout.listitem, parent, false);
            bindView(v, context, cursor);
            return v;
        }
    }

}

<强> listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/displayname"
                android:textSize="18dip"
                android:textStyle="bold"
                android:singleLine="true"
                android:ellipsize="end"/>

        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/title"
                        android:textSize="15dip"
                        android:singleLine="true"
                        android:ellipsize="end"
                        android:layout_weight="1.0"/>

                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/duration"
                        android:gravity="right"
                        android:textSize="15dip"
                        android:singleLine="true"
                        android:ellipsize="end"/>
        </LinearLayout>

</LinearLayout>