In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use VideoView in Android audio and video development". In daily operation, I believe many people have doubts about how to use VideoView in Android audio and video development. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to use VideoView in Android audio and video development". Next, please follow the editor to study!
VideoView introduction
I have previously introduced the use of MediaPlayer+SurfaceView to achieve video playback. Inadvertently found that the official package of VideoView components to achieve simple video playback function, the internal use of MediaPlayer+SurfaceView form to control MediaPlayer to play video files. The use of scenarios is relatively simple, and it is suitable for scenarios that only play videos. Its limited capacity is not suitable for other functions such as adjusting the brightness of videos.
MediaController
In addition to the playback component VideoView, there is also a MediaController component that provides a playback operation bar function for video playback, which can support video playback, pause, fast forward, fast rewind and other functions. There is also a progress bar function that can be dragged to a specified location to play the video.
Use
VideoView encapsulates MediaPlayer and also provides api similar to MediaPlayer. For example, the start method is also used to play video, but it is best to set the setOnpreparedListener callback result before calling this method. When setVideoPath is called, the prepareAsync method will be executed actively. It helps developers encapsulate and implement many functions in VideoView. In fact, it can also learn from its internal source code to achieve a more comprehensive and more complete homemade player.
Commonly used Api description setVideoPath setting video resource start playback pause pausing resume replay seekTo specified location whether to play isPlaying video to get the current playback position setMediaController setting MediaControllersetOnpreparedListener monitoring video load completion event / / instantiation videoView videoView = new VideoView (this); Uri uri = Uri.fromFile ("sdcard/DCIM", "New Century Evangelical Warrior 24.mp4")); / / load video resource videoView.setVideoURI (uri); LinearLayout linearLayout = new LinearLayout (this) LinearLayout.addView (videoView); setContentView (linearLayout); / / set listening videoView.setOnPreparedListener (new MediaPlayer.OnPreparedListener () {@ Override public void onPrepared (MediaPlayer mp) {/ / callback is successful and play video videoView.start ();}}); / / create operation bar MediaController mediaController = new MediaController (this); videoView.setMediaController (mediaController); mediaController.setMediaPlayer (videoView)
Source code analysis
Now that we have encapsulated both VideoView and MediaController components, we have also found many functions that we have tried to achieve before to see how they are implemented.
Progress display
You can see a post (mShowProgress) inside the show method when the MediaController is displayed.
Public void show (int timeout) {if (! mShowing & & mAnchor! = null) {setProgress (); if (mPauseButton! = null) {mPauseButton.requestFocus ();} disableUnsupportedButtons (); updateFloatingWindowLayout (); mWindowManager.addView (mDecor, mDecorLayoutParams); mShowing = true;} updatePausePlay (); / / cause the progress bar to be updated even if mShowing / / was already true. This happens, for example, if we're / / paused with the progress bar showing the user hits play. Post (mShowProgress); if (timeout! = 0 & &! mAccessibilityManager.isTouchExplorationEnabled ()) {removeCallbacks (mFadeOut); postDelayed (mFadeOut, timeout);}}
You can see that mShowProgress is a Runnable, and internal calls are delayed to update setProgress (). The setProgress () method reads the MediaPlayer playback progress to update the playback information.
Private final Runnable mShowProgress = new Runnable () {@ Override public void run () {int pos = setProgress (); if (! mDragging & & mShowing & & mPlayer.isPlaying ()) {postDelayed (mShowProgress, 1000-(pos% 1000));}}; fit the playback size
Previously, the playback size adaptation was implemented by customization, which directly helps developers to achieve video playback adaptation within VideoView. The detailed code can be rewritten in onMeasure directly. The approximate algorithm of the code is to rewrite the width and height of the VideoView by comparing the width and height of the VideoView layout with the width and height of the video.
Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {/ / Log.i ("@", "onMeasure (" + MeasureSpec.toString (widthMeasureSpec) + "," / / + MeasureSpec.toString (heightMeasureSpec) + ")); int width = getDefaultSize (mVideoWidth, widthMeasureSpec); int height = getDefaultSize (mVideoHeight, heightMeasureSpec); if (mVideoWidth > 0 & mVideoHeight > 0) {int widthSpecMode = MeasureSpec.getMode (widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize (widthMeasureSpec) Int heightSpecMode = MeasureSpec.getMode (heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize (heightMeasureSpec); if (widthSpecMode = = MeasureSpec.EXACTLY & & heightSpecMode = = MeasureSpec.EXACTLY) {/ / the size is fixed width = widthSpecSize; height = heightSpecSize; / / for compatibility, we adjust size based on aspect ratio if (mVideoWidth * height
< width * mVideoHeight ) { //Log.i("@@@", "image too wide, correcting"); width = height * mVideoWidth / mVideoHeight; } else if ( mVideoWidth * height >Width * mVideoHeight) {/ / Log.i ("@", "image too tall, correcting"); height = width * mVideoHeight / mVideoWidth;}} else if (widthSpecMode = = MeasureSpec.EXACTLY) {/ / only the width is fixed, adjust the height to match aspect ratio if possible width = widthSpecSize; height = width * mVideoHeight / mVideoWidth If (heightSpecMode = = MeasureSpec.AT_MOST & & height > heightSpecSize) {/ / couldn't match aspect ratio within the constraints height = heightSpecSize;}} else if (heightSpecMode = = MeasureSpec.EXACTLY) {/ / only the height is fixed, adjust the width to match aspect ratio if possible height = heightSpecSize; width = height * mVideoWidth / mVideoHeight If (widthSpecMode = = MeasureSpec.AT_MOST & & width > widthSpecSize) {/ / couldn't match aspect ratio within the constraints width = widthSpecSize;}} else {/ / neither the width nor the height are fixed, try to use actual video size width = mVideoWidth; height = mVideoHeight If (heightSpecMode = = MeasureSpec.AT_MOST & & height > heightSpecSize) {/ / too tall, decrease both width and height height = heightSpecSize; width = height * mVideoWidth / mVideoHeight;} if (widthSpecMode = = MeasureSpec.AT_MOST & & width > widthSpecSize) {/ / too wide, decrease both width and height width = widthSpecSize Height = width * mVideoHeight / mVideoWidth;} else {/ / no size yet, just adopt the given spec sizes} setMeasuredDimension (width, height);} at this point, the study on "how to use VideoView in Android audio and video development" is over. I hope to solve everyone's 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: 254
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.