VLC-based video player

VLC-based video player

I've been researching the video playback function recently. I used VideoView before. I looked it up online and it didn't seem very good. It supported relatively few formats. Nowadays, there are various formats of online videos. I felt that using VideoView to play them was very limited.

We found a suitable player, the Github address is https://github.com/xiaomo/AndroidPlayerLibrary. This player is based on the vlc software. After importing our demo into Eclipse, we can find a libvlc folder.

In the Media class, we can find that there are quite a lot of supported formats.

  1. String[] video_extensions = {
  2. ".3g2" , ".3gp" , ".3gp2" , ".3gpp" , ".amv" , ".asf" , ".avi" , ".divx" , ".drc" , ".dv" ,
  3. ".f4v" , ".flv" , ".gvi" , ".gxf" , ".ismv" , ".iso" , ".m1v" , ".m2v" , ".m2t" , ".m2ts" ,
  4. ".m4v" , ".mkv" , ".mov" , ".mp2" , ".mp2v" , ".mp4" , ".mp4v" , ".mpe" , ".mpeg" ,
  5. ".mpeg1" , ".mpeg2" , ".mpeg4" , ".mpg" , ".mpv2" , ".mts" , ".mtv" , ".mxf" , ".mxg" ,
  6. ".nsv" , ".nut" , ".nuv" , ".ogm" , ".ogv" , ".ogx" , ".ps" , ".rec" , ".rm" , ".rmvb" ,
  7. ".tod" , ".ts" , ".tts" , ".vob" , ".vro" , ".webm" , ".wm" , ".wmv" , ".wtv" , ".xesc" };
  8.  
  9. String[] audio_extensions = {
  10. ".3ga" , ".a52" , ".aac" , ".ac3" , ".adt" , ".adts" , ".aif" , ".aifc" , ".aiff" , ".amr" ,
  11. ".aob" , ".ape" , ".awb" , ".caf" , ".dts" , ".flac" , ".it" , ".m4a" , ".m4b" , ".m4p" ,
  12. ".mid" , ".mka" , ".mlp" , ".mod" , ".mpa" , ".mp1" , ".mp2" , ".mp3" , ".mpc" , ".mpga" ,
  13. ".oga" , ".ogg" , ".oma" , ".opus" , ".ra" , ".ram" , ".rmi" , ".s3m" , ".spx" , ".tta" ,
  14. ".voc" , ".vqf" , ".w64" , ".wav" , ".wma" , ".wv" , ".xa" , ".xm" };

In this example, it writes a PlayerActivity and PlayerView by itself, and implements the IVideoPlayer interface in libvlc in PlayerView

Implement the PlayerView class in PlayerActivity to control the interface and process.

  1. protected   void onCreate(Bundle savedInstanceState) {
  2. super .onCreate(savedInstanceState);
  3. mUrl = getIntent().getStringExtra( "url" );
  4. if (TextUtils.isEmpty(mUrl)) {
  5. Toast.makeText( this , "error:no url in intent!" , Toast.LENGTH_SHORT).show();
  6. return ;
  7. }
  8. requestWindowFeature(Window.FEATURE_NO_TITLE);
  9. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  10.  
  11. setContentView(R.layout.activity_player);
  12.  
  13. mHandler = new Handler( this );
  14.  
  15. tvTitle = (TextView) findViewById(R.id.tv_title);
  16. tvTime = (TextView) findViewById(R.id.tv_time);
  17. tvLength = (TextView) findViewById(R.id.tv_length);
  18. sbVideo = (SeekBar) findViewById(R.id.sb_video);
  19. sbVideo.setOnSeekBarChangeListener( this );
  20. ibLock = (ImageButton) findViewById(R.id.ib_lock);
  21. ibLock.setOnClickListener( this );
  22. ibBackward = (ImageButton) findViewById(R.id.ib_backward);
  23. ibBackward.setOnClickListener( this );
  24. ibPlay = (ImageButton) findViewById(R.id.ib_play);
  25. ibPlay.setOnClickListener( this );
  26. ibFarward = (ImageButton) findViewById(R.id.ib_forward);
  27. ibFarward.setOnClickListener( this );
  28. ibSize = (ImageButton) findViewById(R.id.ib_size);
  29. ibSize.setOnClickListener( this );
  30.  
  31. llOverlay = findViewById(R.id.ll_overlay);
  32. rlOverlayTitle = findViewById(R.id.rl_title);
  33.  
  34. rlLoading = findViewById(R.id.rl_loading);
  35. tvBuffer = (TextView) findViewById(R.id.tv_buffer);
  36. //Usage steps  
  37. //***Step: Get the mPlayerView object through findViewById or new PlayerView()  
  38. //mPlayerView= new PlayerView(PlayerActivity.this);  
  39. mPlayerView = (PlayerView) findViewById(R.id.pv_video);
  40.  
  41. //Step 2: Set parameters in milliseconds  
  42. mPlayerView.setNetWorkCache( 20000 );
  43.  
  44. //Step 3: Initialize the player  
  45. mPlayerView.initPlayer(mUrl);
  46.  
  47. //Step 4: Set up event monitoring, monitor buffer progress, etc.  
  48. mPlayerView.setOnChangeListener( this );
  49.  
  50. //Step 5: Start playing  
  51. mPlayerView.start();
  52.  
  53. //init view  
  54. tvTitle.setText(mUrl);
  55. showLoading();
  56. hideOverlay();
  57.  
  58. }

How to use this library

The library can be used in two ways:

*** Type, directly create a new PlayerView or embed the view in the xml of the layout file.

The second method is to jump to a pre-written playback page PlayerActivity (local file parameter format: file:///sacard/test.rmvb.)

Here I chose the second method

  1. startActivity( new Intent( this , PlayerActivity. class ).putExtra( "url" , items.get(position)));

Through a selection list, click each item and pass a URL to PlayerActivity

Select 1.2JDK download.mp4 from the list

There is no problem switching between horizontal and vertical screens.

<<:  The third lecture on application performance management of Tingyun shared practical experience

>>:  What improvements did I make when I was refining an old project from a few years ago?

Recommend

Five key directions for new media professionals in 2019

WeChat has quietly revamped its version recently!...

Is mortgage loan a farmer loan? What are the requirements for application?

Nowadays, when people are short of money, the fir...

Avoid detours: advice for Java programmers with 1 to 5 years of experience

Today, I am going to share some practical informa...

How to optimize advertising creatives? 3 analysis methods

The impact of advertising materials on advertisin...

Why do you have such a hard time finding seed users?

After the product is launched, someone is needed ...

How to restore physical strength during the recovery period? Expert answers!

How to use medicine scientifically after infectio...