首页 > 代码库 > 播放视频
播放视频
VideoView播放视频常见方法:
关键代码:
public class MainActivity extends AppCompatActivity { private Button play,pause,stop; private VideoView videoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); videoView = (VideoView)findViewById(R.id.videoview); initVideo(); // 动态申请授权 if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1); } } protected void myClick(View v){ switch (v.getId()){ case R.id.play: if( !videoView.isPlaying() ){ videoView.start(); } break; case R.id.pause: if( videoView.isPlaying() ){ videoView.pause(); } break; case R.id.replay: if( videoView.isPlaying() ){ videoView.resume(); } break; default: break; } } private void initVideo(){ try{ File file = new File(Environment.getExternalStorageDirectory()+"/Guo","a.mp4"); videoView.setVideoPath(file.getAbsolutePath()); }catch (Exception e){ e.printStackTrace(); } } }
所需权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
播放视频
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。