android之视频的播放(VedioView,SuefaceView)和图片的获得

来源:互联网 发布:win7日程安排软件 编辑:程序博客网 时间:2024/06/07 14:06

1、视频播放

方式一:直接用VideoView
布局文件:

<LinearLayout 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"    android:orientation="vertical"    tools:context=".MainActivity"><Button    android:id="@+id/button_start"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="播放"/><VideoView    android:id="@+id/videoview"    android:layout_width="wrap_content"    android:layout_height="0dp"    android:layout_weight="1"/></LinearLayout>

Activity

public class MainActivity extends Activity {    private Button mButton;    private VideoView mVideoView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mButton = (Button) findViewById(R.id.button_start);        mVideoView = (VideoView) findViewById(R.id.videoview);        mButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                mVideoView.setVideoPath(Environment.getExternalStorageDirectory() + "/shi.3gp");//找到sdcard下的shi.3gp的视频                mVideoView.setMediaController(new MediaController(MainActivity.this));                mVideoView.start();            }        });    }}

方式二:SurfaceView和Media
布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="bofang"/>    <SurfaceView        android:id="@+id/surfaceview"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>

Activity

public class SurfaceViewActivity extends Activity {    private Button mButton;    private SurfaceView mSurfaceViw;    private MediaPlayer player;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_surfaceview);        mButton = (Button) findViewById(R.id.button);        mSurfaceViw = (SurfaceView) findViewById(R.id.surfaceview);        mButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if(player==null){                    player = new MediaPlayer();                }                player.reset();//记得要重置                try {                    //设置视频地址                    player.setDataSource(Environment.getExternalStorageDirectory()+"/DCIM/hh.3gp");//视频来源是sdcard下的DCIM中的hh.3gp文件                    player.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置播放声音类型                    player.setDisplay(mSurfaceViw.getHolder());//设置视频播放位置                    player.prepareAsync();//准备                    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {                        @Override                        public void onPrepared(MediaPlayer mp) {                            mp.start();                        }                    });                } catch (IOException e) {                    e.printStackTrace();                }            }        });    }}

图片的获得(1、即拍即得2、从手机相册中获得)

布局文件:

<LinearLayout 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"    android:orientation="vertical"    android:gravity="center"    tools:context=".MainActivity">    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="拍照" /> <Button        android:id="@+id/button_getpicture"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="得到相册里的图片"        android:layout_alignParentRight="true"/><ImageView        android:id="@+id/imageview"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>
public class MainActivity extends Activity {    private Button mButton;    private ImageView imageView;    private File file;      private  Button mButtonGet;    public static final int GETPIC_FROM_CAMERA=0x23;    public static final int GETPIC_FROM_GELLAY=0x24;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mButton = (Button) findViewById(R.id.button);        imageView = (ImageView) findViewById(R.id.imageview);         mButtonGet = (Button) findViewById(R.id.button_getpicture);        mButtonGet.setOnClickListener(new View.OnClickListener() {//得到相册里的照片            @Override            public void onClick(View v) {                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);                intent.setType("image/*"); /* 开启Pictures画面Type设定为image */                file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");                if (!file.exists()) {                    try {                        file.createNewFile();                    } catch (IOException e) {                        e.printStackTrace();                    }                }                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//设置照片存放的位置                startActivityForResult(intent,GETPIC_FROM_GELLAY);            }        });        //从应用中拍照得到图片        mButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent();                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//隐式启动系统相机                file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");                try {                    file.createNewFile();                } catch (IOException e) {                    e.printStackTrace();                }                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//设置照片存放的位置                startActivityForResult(intent, 0x23);//开始启动            }        });    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);         if (resultCode == RESULT_OK) {            switch (requestCode){                case GETPIC_FROM_CAMERA:                    ImageZip.zipImage(file.getAbsolutePath());                    imageView.setImageURI(Uri.fromFile(file));//得到图片                    break;                case GETPIC_FROM_GELLAY:                    Uri uri = data.getData();                    imageView.setImageURI(uri);                    break;            }        }    }}
0 0
原创粉丝点击