通过VideoView播放视频的步骤:
1、在界面布局文件中定义VideoView组件,或在程序中创建VideoView组件
2、调用VideoView的如下两个方法来加载指定的视频
setVidePath(String path):加载path文件代表的视频
setVideoURI(Uri uri):加载uri所对应的视频
3、调用VideoView的start()、stop()、psuse()方法来控制视频的播放
VideoView通过与MediaController类结合使用,开发者可以不用自己控制播放与暂停
简单实例:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <VideoView android:id="@+id/video1" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
package com.android.xiong.vidoviewtest; import java.io.File; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.MediaController; import android.widget.VideoView; public class MainActivity extends Activity { private VideoView video1; MediaController mediaco; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); video1=(VideoView)findViewById(R.id.video1); mediaco=new MediaController(this); File file=new File("/mnt/sdcard/通话录音/1.mp4"); if(file.exists()){ //VideoView与MediaController进行关联 video1.setVideoPath(file.getAbsolutePath()); video1.setMediaController(mediaco); mediaco.setMediaPlayer(video1); //让VideiView获取焦点 video1.requestFocus(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }