-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial implementation over vertical videos using SurfaceView * Rename + add ability to preload Surfaces * update gradle + viewpager sample included
- Loading branch information
1 parent
c1cfc52
commit b1093ce
Showing
21 changed files
with
507 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
.../main/java/com/android/textureview_surfaceview/examples/verticalpager/HDRVerticalPager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.android.textureview_surfaceview.examples.verticalpager | ||
|
||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.viewpager2.widget.ViewPager2 | ||
import com.android.textureview_surfaceview.databinding.HdrVerticalPagerBinding | ||
|
||
class HDRVerticalPager : AppCompatActivity() { | ||
|
||
private lateinit var binding: HdrVerticalPagerBinding | ||
private lateinit var viewPager: ViewPager2 | ||
private lateinit var adapter: HDRVerticalPagerAdapter | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding = HdrVerticalPagerBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
|
||
/// Set up view pager & adapter | ||
setUpViewPager2() | ||
} | ||
|
||
private fun setUpViewPager2() { | ||
viewPager = binding.hdrVerticalViewpager | ||
viewPager.offscreenPageLimit = 1 | ||
adapter = HDRVerticalPagerAdapter(this) | ||
viewPager.adapter = adapter | ||
|
||
viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { | ||
override fun onPageScrollStateChanged(state: Int) { | ||
if (state != ViewPager2.SCROLL_STATE_DRAGGING) { | ||
Log.w("Testing", "Making the happen on " + viewPager.currentItem) | ||
return | ||
} | ||
|
||
val position = viewPager.currentItem | ||
|
||
viewPager.findFragmentAtPosition( | ||
supportFragmentManager, | ||
position - 1 | ||
)?.let { | ||
(it as HDRVerticalPagerFragmentInterface).setSurfaceViewVisibility(true) | ||
} | ||
|
||
viewPager.findFragmentAtPosition( | ||
supportFragmentManager, | ||
position + 1 | ||
)?.let { | ||
(it as HDRVerticalPagerFragmentInterface).setSurfaceViewVisibility(true) | ||
} | ||
} | ||
}) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ava/com/android/textureview_surfaceview/examples/verticalpager/HDRVerticalPagerAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.android.textureview_surfaceview.examples.verticalpager | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentManager | ||
import androidx.viewpager2.adapter.FragmentStateAdapter | ||
import androidx.viewpager2.widget.ViewPager2 | ||
|
||
class HDRVerticalPagerAdapter(activity: AppCompatActivity) : FragmentStateAdapter(activity) { | ||
override fun getItemCount() = 3 | ||
override fun createFragment(position: Int) = HDRVerticalPagerFragmentSurfaceView.newInstance(position) | ||
} | ||
|
16 changes: 16 additions & 0 deletions
16
.../com/android/textureview_surfaceview/examples/verticalpager/HDRVerticalPagerExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.android.textureview_surfaceview.examples.verticalpager | ||
|
||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentManager | ||
import androidx.viewpager2.widget.ViewPager2 | ||
|
||
fun ViewPager2.findCurrentFragment(fragmentManager: FragmentManager): Fragment? { | ||
return fragmentManager.findFragmentByTag("f$currentItem") | ||
} | ||
|
||
fun ViewPager2.findFragmentAtPosition( | ||
fragmentManager: FragmentManager, | ||
position: Int | ||
): Fragment? { | ||
return fragmentManager.findFragmentByTag("f$position") | ||
} |
7 changes: 7 additions & 0 deletions
7
...droid/textureview_surfaceview/examples/verticalpager/HDRVerticalPagerFragmentInterface.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.android.textureview_surfaceview.examples.verticalpager | ||
|
||
interface HDRVerticalPagerFragmentInterface { | ||
fun setSurfaceViewVisibility(visibility: Boolean) { | ||
// Do nothing implementation | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...oid/textureview_surfaceview/examples/verticalpager/HDRVerticalPagerFragmentSurfaceView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.android.textureview_surfaceview.examples.verticalpager | ||
|
||
import android.content.res.AssetFileDescriptor | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.SurfaceHolder | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import com.android.textureview_surfaceview.Constants | ||
import com.android.textureview_surfaceview.databinding.FragmentHdrVerticalPagerSurfaceviewBinding | ||
import com.android.textureview_surfaceview.decoder.CustomVideoDecoder | ||
|
||
class HDRVerticalPagerFragmentSurfaceView : Fragment(), HDRVerticalPagerFragmentInterface, | ||
SurfaceHolder.Callback { | ||
|
||
private lateinit var binding: FragmentHdrVerticalPagerSurfaceviewBinding | ||
private var decoder: CustomVideoDecoder? = null | ||
private var asset: AssetFileDescriptor? = null | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
val pos = arguments?.getInt(POSITION_ARG) | ||
pos?.let { | ||
when (it) { | ||
0 -> asset = context?.assets?.openFd(Constants.HDR_VERTICAL_VIDEO_1) | ||
1 -> asset = context?.assets?.openFd(Constants.HDR_VERTICAL_VIDEO_2) | ||
2 -> asset = context?.assets?.openFd(Constants.HDR_VERTICAL_VIDEO_3) | ||
} | ||
} | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
binding = FragmentHdrVerticalPagerSurfaceviewBinding.inflate(layoutInflater) | ||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
binding.hdrVerticalViewpagerSurfaceView.setAspectRatio(9, 16) | ||
binding.hdrVerticalViewpagerSurfaceView.holder.addCallback(this) | ||
} | ||
|
||
override fun onResume() { | ||
binding.hdrVerticalViewpagerSurfaceView.visibility = View.VISIBLE | ||
super.onResume() | ||
} | ||
|
||
override fun onPause() { | ||
decoder?.stop() | ||
binding.hdrVerticalViewpagerSurfaceView.visibility = View.INVISIBLE | ||
super.onPause() | ||
} | ||
|
||
companion object { | ||
var POSITION_ARG = "position_arg" | ||
|
||
@JvmStatic | ||
fun newInstance(position: Int) = HDRVerticalPagerFragmentSurfaceView().apply { | ||
arguments = Bundle().apply { | ||
putInt(POSITION_ARG, position) | ||
} | ||
} | ||
} | ||
|
||
override fun surfaceCreated(holder: SurfaceHolder) { | ||
asset?.let { | ||
decoder = CustomVideoDecoder.buildWithAssetFile(it) | ||
decoder?.setSurface(holder.surface) | ||
decoder?.start(loop = true) | ||
} | ||
} | ||
|
||
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) { | ||
} | ||
|
||
override fun surfaceDestroyed(holder: SurfaceHolder) { | ||
decoder?.stop() | ||
} | ||
|
||
override fun setSurfaceViewVisibility(visibility: Boolean) { | ||
binding.hdrVerticalViewpagerSurfaceView.visibility = | ||
when (visibility) { | ||
false -> View.INVISIBLE | ||
true -> View.VISIBLE | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...oid/textureview_surfaceview/examples/verticalpager/HDRVerticalPagerFragmentTextureView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.android.textureview_surfaceview.examples.verticalpager | ||
|
||
import android.content.res.AssetFileDescriptor | ||
import android.graphics.SurfaceTexture | ||
import android.os.Bundle | ||
import android.view.* | ||
import androidx.fragment.app.Fragment | ||
import com.android.textureview_surfaceview.Constants | ||
import com.android.textureview_surfaceview.databinding.FragmentHdrVerticalPagerTextureviewBinding | ||
import com.android.textureview_surfaceview.decoder.CustomVideoDecoder | ||
|
||
class HDRVerticalPagerFragmentTextureView : | ||
Fragment(), | ||
HDRVerticalPagerFragmentInterface, | ||
TextureView.SurfaceTextureListener { | ||
|
||
private lateinit var binding: FragmentHdrVerticalPagerTextureviewBinding | ||
private var decoder: CustomVideoDecoder? = null | ||
private var asset: AssetFileDescriptor? = null | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
val pos = arguments?.getInt(POSITION_ARG) | ||
pos?.let { | ||
when (it) { | ||
0 -> asset = context?.assets?.openFd(Constants.HDR_VERTICAL_VIDEO_1) | ||
1 -> asset = context?.assets?.openFd(Constants.HDR_VERTICAL_VIDEO_2) | ||
2 -> asset = context?.assets?.openFd(Constants.HDR_VERTICAL_VIDEO_3) | ||
} | ||
} | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
binding = FragmentHdrVerticalPagerTextureviewBinding.inflate(layoutInflater) | ||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
binding.hdrVerticalViewpagerTextureView.surfaceTextureListener = this | ||
binding.hdrVerticalViewpagerTextureView.setAspectRatio(9, 16) | ||
} | ||
|
||
companion object { | ||
var POSITION_ARG = "position_arg" | ||
|
||
@JvmStatic | ||
fun newInstance(position: Int) = HDRVerticalPagerFragmentTextureView().apply { | ||
arguments = Bundle().apply { | ||
putInt(POSITION_ARG, position) | ||
} | ||
} | ||
} | ||
|
||
override fun onSurfaceTextureAvailable( | ||
surfaceTexture: SurfaceTexture, | ||
width: Int, | ||
height: Int | ||
) { | ||
asset?.let { | ||
decoder = CustomVideoDecoder.buildWithAssetFile(it) | ||
decoder?.setSurface(Surface(surfaceTexture)) | ||
decoder?.start(loop = true) | ||
} | ||
} | ||
|
||
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) { | ||
// TODO("Not yet implemented") | ||
} | ||
|
||
override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean { | ||
decoder?.stop() | ||
return true | ||
} | ||
|
||
override fun onSurfaceTextureUpdated(surface: SurfaceTexture) { | ||
|
||
} | ||
} |
Oops, something went wrong.