android中使用videoview播放视频

【android中使用videoview播放视频】与天地兮比寿,与日月兮齐光。这篇文章主要讲述android中使用videoview播放视频相关的知识,希望能为你提供帮助。
先是布局文件:

1 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2xmlns:tools="http://schemas.android.com/tools" 3android:layout_width="match_parent" 4android:layout_height="match_parent" 5android:orientation="vertical" 6tools:context=".MainActivity" > 7 8< VideoView 9android:id="@+id/video" 10android:layout_width="match_parent" 11android:layout_height="match_parent" /> 12 13 < /LinearLayout>

然后在mainactivity中使用videoview:
1 import java.io.File; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.os.Environment; 6 import android.widget.MediaController; 7 import android.widget.Toast; 8 import android.widget.VideoView; 9 10 public class MainActivity extends Activity { 11VideoView videoView; 12MediaController mController; 13 14@Override 15protected void onCreate(Bundle savedInstanceState) { 16super.onCreate(savedInstanceState); 17setContentView(R.layout.activity_main); 18videoView = (VideoView) findViewById(R.id.video); 19mController = new MediaController(this); 20File video = new File(Environment.getExternalStorageDirectory() + "/xiexienideai.mp4"); //获得手机上sd卡视频存储路径 21if(video.exists()){ 22videoView.setVideoPath(video.getAbsolutePath()); 23videoView.setMediaController(mController); //设置mController关联videoview 24mController.setMediaPlayer(videoView); 25videoView.requestFocus(); //videoview获得焦点 26}else{ 27Toast.makeText(this, "nonono", Toast.LENGTH_LONG).show(); 28} 29} 30 31 }

由于使用到sd卡上内容获取,需要在配置文件中增加权限:
< uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    推荐阅读