public class MainActivity extends Activity { private VideoView video; private String path=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); video = (VideoView) findViewById(R.id.videoview); // assets读取视频文件 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File file = Environment.getExternalStorageDirectory(); File file2 = new File(file.getAbsoluteFile()+"/video.mp4"); path = file2.getAbsolutePath(); try { InputStream is = getAssets().open("video1.mp4"); FileOutputStream fo = new FileOutputStream(file2); byte[] b=new byte[1024]; while (true) { int m = is.read(b); if(m==-1) break; fo.write(b, 0, m); } fo.flush(); is.close(); fo.close(); } catch (IOException e) { e.printStackTrace(); } } // 设置视频文件的路径 video.setVideoPath(path); // raw读取视频播放 // video.setVideoPath("android.resource://"+getPackageName()+"/"+R.raw.video1); findViewById(R.id.start).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { MediaController media=new MediaController(MainActivity.this); video.setMediaController(media); media.setAnchorView(video); video.start(); } }); } }
视频播放,Raw和assets文件夹
最新推荐文章于 2023-02-04 20:33:01 发布