Draggable floating window video zoom

Directly on the map

    

Can be scaled three floating window and drag the video

A suspension window

1.1, create WindowManager

        // Create params, the size of the position control 
mParams = WindowManager.LayoutParams ()
// floating window mWindowManager
= application.getSystemService (Context.WINDOW_SERVICE) The WindowManager AS
// permission request mParams.type
= IF (Build.VERSION.SDK_INT> = the Build. VERSION_CODES.O) { WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY } the else { WindowManager.LayoutParams.TYPE_SYSTEM_ALERT }

1.2 to create a window interface

View includes a custom video player controls, turn off the controls and zoom controls

1.3 Add View

        mParams.width = mSmallWidth
        mParams.height = mSmallHeight
        mParams.y = mHeight
        mWindowManager.addView(mVideoView, mParams)

This creates a floating window, width height controlled by controlling the size of Params, by controlling Param x, y position of the control

Second, the video player

2.1 VideoVIew Play

VideoVIew directly control video playback

mVideoView.setVideoURI(path)
mVideoView.start()
mVideoView.requestFocus()
mVideoView.setOnCompletionListener {
      mVideoView.start()
}

Uri major local files is not very good acquisition, currently available through FileProvider, directly Uri.parse () can not find the case file will get

val uri = FileProvider.getUriForFile(this, "com.js.floatingwindow.fileprovider", File(path))

2.2 SurfaceView Play

SurfaceView + MediaPlayer play, VideoView more complex than, but the same underlying this way in VideoVIew

Play using MediaPlayer, SurfaceView on display image, to correlate by Player.setDisplay (holder) both, music players and other small difference

        mPlayer.isLooping = true
        try {
            mPlayer.setDatSource(path.toString())
        } catch (e: Exception) {

        }
        mPlayer.prepareAsync()
        mPlayer.setOnPreparedListener {
            mPlayer.start()
        }

2.3 Zoom

When the zoom, video needs to follow the changes, this change in the way the two are not the same

VideoView relatively simple

        viewTreeObserver.addOnGlobalLayoutListener {
            mVideoView.run {
                holder.setFixedSize(width,height)
                requestLayout()
            }
        }

SurfaceView 

fun changeVideoSize() {
        try {
            var width: Int = mPlayer.videoWidth
            var height: Int = mPlayer.videoHeight

            val surfaceWidth = getWidth()
            val surfaceHeight = getHeight()

            val max: Float = Math.max(width / surfaceWidth.toFloat(), height / surfaceHeight.toFloat())

            width = Math.ceil((width / max).toDouble()).toInt()
            height = Math.ceil((height / max).toDouble()).toInt()

            val params = FrameLayout.LayoutParams(width, height)
            params.gravity = Gravity.CENTER
            mVideoView.layoutParams = params
            mVideoView.invalidate()
        } catch (e: IllegalStateException) {
            //mPlayer可能出现异常
        }

    }

Useful words help add a star Oh, welcome to the discussion

 Portal

 

 

Guess you like

Origin www.cnblogs.com/doubleyoujs/p/11495044.html