Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Android to play video

Shulou Source: shulou.com Published: 2022-06-02 20:54:27 11月15日 Update

This article mainly introduces "how to achieve Android video playback". In daily operation, I believe many people have doubts about how to achieve Android video playback. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to achieve Android video playback". Next, please follow the editor to study!

File file = new File (Environment.getExternalStorageDirectory (), path); videoView.setVideoPath (file.getPath ()); / / specify the path to the video file videoView.start (); / / start playing videoView.pause (); / / pause playback videoView.resume (); / / replay

Complete code

/ / Video playback public class MainActivity extends AppCompatActivity {private VideoView videoView; private Button play; private Button pause; private Button replay; / * set view playback control bar * / private MediaController mediaController; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); initView (); initData () } private void initView () {videoView = (VideoView) findViewById (R.id.video_view); play = (Button) findViewById (R.id.play); pause = (Button) findViewById (R.id.pause); replay = (Button) findViewById (R.id.replay); / initialize videoview control bar / / mediaController=new MediaController (this) / set videoview control bar / / videoView.setMediaController (mediaController); / set display control bar / / mediaController.show (0) } private void initData () {if (ContextCompat.checkSelfPermission (MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)! = PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions (MainActivity.this, new String [] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);} else {initVideoPath ("123.mp4") / / initialize MediaPlayer} play.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {if (! videoView.isPlaying ()) {videoView.start (); / / start playback}) Pause.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {if (videoView.isPlaying ()) {videoView.pause (); / / pause playback}) Replay.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {if (videoView.isPlaying ()) {videoView.resume (); / / replay}) } private void initVideoPath (String path) {File file = new File (Environment.getExternalStorageDirectory (), path); videoView.setVideoPath (file.getPath ()) / / specify the path to the video file} @ Override public void onRequestPermissionsResult (int requestCode, String [] permissions, int [] grantResults) {switch (requestCode) {case 1: if (grantResults.length > 0 & & grantResults [0] = = PackageManager.PERMISSION_GRANTED) {initVideoPath ("movie.mp4") } else {Toast.makeText (this, "denied permission will not be able to use the program", Toast.LENGTH_SHORT) .show (); finish ();} break; default:}} @ Override protected void onDestroy () {super.onDestroy () If (videoView! = null) {videoView.suspend ();}

Corresponding xml:

At this point, the study on "how to play video on Android" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Videos learning control files more paths help utility next code articles methods permissions theories knowledge programs articles websites materials follow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info Linux macOS MariaDB