Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Android to play video

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report