Get the App
SLTechnology News&Howtos  ›  Development  › 

How to play videos using VideoView in Android

Shulou Source: shulou.com Published: 2022-06-02 11:27:58 10月19日 Update

Editor to share with you how Android uses VideoView to play videos, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Give Android VideoView a file directory and you can play video files directly from smart devices. Now take test.mp4, a video file that was shot and renamed with your phone beforehand, as an example.

(1) you need to write a ViedoView in the layout file:

(2) Don't forget to add permission to read and write external storage in the AndroidManifest.xml file:

(3) set the file directory for VideoView in the java code, and then play it on start:

Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); VideoView videoView = (VideoView) findViewById (R.id.videoView); / / the path obtained is equal to: / storage/emulated/0/DCIM File path = Environment .getExternalStoragePublicDirectory (Environment.DIRECTORY_DCIM) / / stitching the full path File f = new File (path, "/ Camera/test.mp4"); / / f.getAbsolutePath () = / storage/emulated/0/DCIM//\ Camera/test.mp4 videoView.setVideoPath (f.getAbsolutePath ()) at this time; / / starting video playback videoView.start (); / / VideiView gaining focus / / videoView.requestFocus () }} above is all the content of the article "how to play videos with Android using VideoView". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Files videos articles content directories not much code most layout mobile phones intelligence more permissions focus knowledge industry equipment information information channels paths Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Technology Docker Linux Shulou Tech Info