Skip to content

Commit

Permalink
Added zoom & pan capability.
Browse files Browse the repository at this point in the history
  • Loading branch information
zegkljan committed Sep 11, 2022
1 parent 1b6718f commit 6e45cbe
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 21 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ dependencies {
implementation "com.google.android.exoplayer:exoplayer-core:$exoplayer_version"
implementation "com.google.android.exoplayer:exoplayer-ui:$exoplayer_version"

// Zoomable view
implementation "com.otaliastudios:zoomlayout:1.9.0"

// Vertical seekbar
implementation 'com.h6ah4i.android.widget.verticalseekbar:verticalseekbar:1.0.0'

Expand Down
103 changes: 103 additions & 0 deletions app/src/main/java/cz/zegkljan/videoreferee/ZoomPlayerView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package cz.zegkljan.videoreferee

import android.content.Context
import android.graphics.Matrix
import android.opengl.GLSurfaceView
import android.util.AttributeSet
import android.util.Log
import android.view.SurfaceView
import android.view.View
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.ui.PlayerView
import com.google.android.exoplayer2.video.VideoSize
import com.otaliastudios.zoom.ZoomEngine
import com.otaliastudios.zoom.ZoomSurfaceView

class ZoomPlayerView(context: Context, attrs: AttributeSet?) : PlayerView(context, attrs) {
private var isZoomedIn: Boolean = false
private val surfaceView: ZoomSurfaceView = findViewById(R.id.player_surface_view)

init {
val zoomEngineListener = object : ZoomEngine.Listener {
override fun onIdle(engine: ZoomEngine) {
showController()
}

override fun onUpdate(engine: ZoomEngine, matrix: Matrix) {
hideController()
/*
isZoomedIn = engine.zoom > 1
val finalResizeMode = if (isZoomedIn)
AspectRatioFrameLayout.RESIZE_MODE_FILL
else
AspectRatioFrameLayout.RESIZE_MODE_FIT
if (finalResizeMode != resizeMode) {
resizeMode = finalResizeMode
}
*/
}
}

val playerSurfaceViewListener = object : Player.Listener {
override fun onVideoSizeChanged(videoSize: VideoSize) {
surfaceView.setContentSize(videoSize.width.toFloat(), videoSize.height.toFloat())
}
}

surfaceView.addCallback(object : ZoomSurfaceView.Callback {
override fun onZoomSurfaceCreated(view: ZoomSurfaceView) {
view.engine.addListener(zoomEngineListener)
player?.setVideoSurface(view.surface)
player?.addListener(playerSurfaceViewListener)
}

override fun onZoomSurfaceDestroyed(view: ZoomSurfaceView) {
//view.engine.removeListener(zoomEngineListener)
player?.removeListener(playerSurfaceViewListener)
player?.setVideoSurface(null)
}
})
}

override fun setPlayer(player: Player?) {
super.setPlayer(player)

if (player == null) {
return
}
if (player.isCommandAvailable(Player.COMMAND_SET_VIDEO_SURFACE)) {
if (surfaceView is SurfaceView) {
player.setVideoSurfaceView(surfaceView as SurfaceView)
}
}
if (player.isCommandAvailable(Player.COMMAND_GET_TEXT)) {
subtitleView?.setCues(player.currentCues)
}
}

override fun setVisibility(visibility: Int) {
super.setVisibility(visibility)
if (surfaceView is SurfaceView) {
// Work around https://github.com/google/ExoPlayer/issues/3160.
surfaceView.visibility = visibility
}
}

override fun getVideoSurfaceView(): View {
return surfaceView
}

override fun onResume() {
if (surfaceView is GLSurfaceView) {
(surfaceView as GLSurfaceView).onResume()
}
}

override fun onPause() {
if (surfaceView is GLSurfaceView) {
(surfaceView as GLSurfaceView).onPause()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import androidx.navigation.fragment.navArgs
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.SimpleExoPlayer
import cz.zegkljan.videoreferee.R
import cz.zegkljan.videoreferee.databinding.FragmentPlayerBinding
import cz.zegkljan.videoreferee.utils.BOUT_COUNTER_KEY
Expand All @@ -59,7 +58,7 @@ class PlayerFragment : Fragment() {
}

/** Player */
private var player: SimpleExoPlayer? = null
private var player: ExoPlayer? = null
private val playbackStateListener: Player.Listener = object : Player.Listener {
override fun onPlaybackStateChanged(playbackState: Int) {
val stateString: String = when (playbackState) {
Expand Down Expand Up @@ -99,7 +98,7 @@ class PlayerFragment : Fragment() {
private fun initializePlayer() {
mspf = (1000f / args.fps).roundToInt()

player = SimpleExoPlayer.Builder(requireContext())
player = ExoPlayer.Builder(requireContext())
.build()
.also { exoPlayer ->
fragmentPlayerBinding.playerView.player = exoPlayer
Expand Down Expand Up @@ -212,7 +211,7 @@ class PlayerFragment : Fragment() {
private fun releasePlayer() {
player?.run {
playbackPosition = this.currentPosition
currentWindow = this.currentWindowIndex
currentWindow = this.currentMediaItemIndex
plWhenReady = this.playWhenReady
removeListener(playbackStateListener)
release()
Expand Down
85 changes: 85 additions & 0 deletions app/src/main/res/layout/exo_player_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<com.google.android.exoplayer2.ui.AspectRatioFrameLayout
android:id="@id/exo_content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">

<!-- Video surface will be inserted as the first child of the content frame. -->
<com.otaliastudios.zoom.ZoomSurfaceView
android:id="@+id/player_surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleX="1"
android:scaleY="1"
app:maxZoom="6"
app:minZoom="1"
app:minZoomType="zoom" />

<View
android:id="@id/exo_shutter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black" />

<ImageView
android:id="@id/exo_artwork"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />

<com.google.android.exoplayer2.ui.SubtitleView
android:id="@id/exo_subtitles"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<ProgressBar
android:id="@id/exo_buffering"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true" />

<TextView
android:id="@id/exo_error_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/exo_error_message_background_color"
android:gravity="center"
android:padding="16dp" />

</com.google.android.exoplayer2.ui.AspectRatioFrameLayout>

<FrameLayout
android:id="@id/exo_ad_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<FrameLayout
android:id="@id/exo_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<View
android:id="@id/exo_controller_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</merge>
34 changes: 17 additions & 17 deletions app/src/main/res/layout/fragment_player.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2021 Jan Žegklitz
~
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,42 +13,42 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.exoplayer2.ui.PlayerView
<cz.zegkljan.videoreferee.ZoomPlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
app:surface_type="none" />

<Button
android:id="@+id/done_button"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginVertical="32dp"
android:layout_marginHorizontal="48dp"
android:layout_marginVertical="32dp"
android:background="@drawable/ic_baseline_check_24"
android:contentDescription="@string/done"
android:scaleType="fitCenter"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="RtlHardcoded" />

<Button
android:id="@+id/delete_button"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginVertical="32dp"
android:layout_marginHorizontal="16dp"
android:layout_marginVertical="32dp"
android:background="@drawable/ic_baseline_delete_24"
android:contentDescription="@string/delete"
android:scaleType="fitCenter"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="@id/done_button"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="RtlHardcoded" />

<TextView
Expand All @@ -59,24 +58,25 @@
android:layout_marginVertical="32dp"
android:textAlignment="center"
android:textSize="16sp"
app:layout_constraintTop_toBottomOf="@id/done_button"
app:layout_constraintLeft_toLeftOf="@id/done_button"
app:layout_constraintRight_toRightOf="@id/done_button"/>
app:layout_constraintRight_toRightOf="@id/done_button"
app:layout_constraintTop_toBottomOf="@id/done_button" />

<com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBarWrapper
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="120dp"
android:layout_marginBottom="64dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="@id/done_button"
app:layout_constraintRight_toRightOf="@id/done_button"
app:layout_constraintTop_toBottomOf="@id/playback_speed_text"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="120dp"
android:layout_marginBottom="64dp">
app:layout_constraintTop_toBottomOf="@id/playback_speed_text">

<com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBar
android:id="@+id/playback_speed_seek"
android:max="100"
android:layout_width="0dp"
android:layout_height="0dp"
android:max="100"
android:splitTrack="false"
app:seekBarRotation="CW270" />
</com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBarWrapper>
Expand Down

0 comments on commit 6e45cbe

Please sign in to comment.