在Android中播放视频的例子 ---亲测!

1 xml文件 命名:videoplayer

主Activity命名:VideoActivity

将视频文件放在手机  sdcard/ceshi.3gp  注意视频名字和格式

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        androidrientation="vertical" 

        android:layout_width="fill_parent"

        android:layout_height="fill_parent">

<TextView  

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="@string/hello"

    />

    

        <VideoView 

        android:id="@+id/video_view" 

        android:layout_width="fill_parent"

        android:layout_height="300px" 

        />

        

        <Button android:id="@+id/cmd_load"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="load"

    />  

  <Button android:id="@+id/cmd_play"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="这是一个视频"

    />

  <Button android:id="@+id/cmd_pause"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="pause"

   />

        

</LinearLayout>

 

 

2、java代码 package com.eoeandroid.demo.VideoPlayer;



import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.VideoView;



public class VideoActivity extends Activity {

        private static final String TAG = "VideoActivity";



        public void onCreate(Bundle icicle) {

                super.onCreate(icicle);

                setTitle("VideoActivity");

                Log.v(TAG, "onCreate: ===> ");

                setContentView(R.layout.videoplayer);

                final VideoView view = (VideoView) findViewById(R.id.video_view);



                Button cmdload = (Button) this.findViewById(R.id.cmd_load);

                cmdload.setOnClickListener(new OnClickListener() {

                        public void onClick(View arg0) {

                                // TODO Auto-generated method stub

                                Log.v(TAG, "setVideoPath: ===> ");

                                view.setVideoPath("/sdcard/ceshiu.3gp");

                        }



                });



                Button cmdplay = (Button) this.findViewById(R.id.cmd_play);

                cmdplay.setOnClickListener(new OnClickListener() {

                        public void onClick(View arg0) {

                                // TODO Auto-generated method stub

                                Log.v(TAG, "start: ===> ");

                                view.start();

                        }

                });



                Button cmdpause = (Button) this.findViewById(R.id.cmd_pause);

                cmdpause.setOnClickListener(new OnClickListener() {

                        public void onClick(View arg0) {

                                // TODO Auto-generated method stub

                                Log.v(TAG, "pause: ===> ");

                                view.pause();

                        }

                });

        }

}

 

 

 

复制代码也比较简单那,获取VideoView ,然后用几个按钮分别演示下加载视频和播放、暂停的动作。

需要注意的是:
视频文件最后别太大,我用了个6M的,加载起来还可以。
视频文件最后是放SD卡上,别一起打包。

你可以在这个基础上,加些快进、快退之类的控制按钮。

posted @ 2015-04-17 16:39  蜗牛快跑zzz  阅读(336)  评论(0编辑  收藏  举报