안녕하세요. 현재 유튜브 링크를 클릭하면 웹뷰로 실행되도록 구현해야합니다.정상적으로 링크클릭 시 모바일유튜브화면이 뜨며, 전체화면 버튼이 클릭되지않아 구글링을 통해 전체화면을 구현하였습니다.
전체화면상태에서 화면회전할때 가로세로 상관없이, 간헐적으로 유튜브 영상 자체가 짤립니다. (설정버튼이나 더보기버튼 같은 경우 match에 맞게 된거보면 영상 자체만 짤리는 거 같습니다) 해당부분을 어떻게 잡아야할지 감이 안옵니다...


class YoutubeWebActivity : BaseActivity<ActivityYoutubeWebBinding>() {
private var webUrl: String = ""
private var mCustomView: View? = null
private var mWebChromeClient: YoutubeWebChromeClient? = null
private var isAutoRotation = false
@LayoutRes
override fun getLayoutResId() = R.layout.activity_youtube_web
override fun onResume() {
super.onResume()
binding.youtubeWebView.onResume()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.activity = this
binding.lifecycleOwner = this
hideStatusBar()
initData()
initObserve()
initView()
}
private fun initData() {
intent.extras?.let {
webUrl = it.getString(Const.EXTRA_DATA_WEB_URL, "")
}
}
override fun initObserve() {
// Do nothing...
}
@SuppressLint("SetJavaScriptEnabled")
private fun initView() = with(binding) {
youtubeWebView.apply {
webViewClient = WebViewClient()
webChromeClient = YoutubeWebChromeClient().apply {
mWebChromeClient = this
}
settings.javaScriptEnabled = true
settings.javaScriptCanOpenWindowsAutomatically = true // 웹뷰내의 JS의 window.open()을 허용할 것인지에 대한 여부
loadUrl(webUrl)
setWebContentsDebuggingEnabled(true)
}
}
override fun onBackPressed() {
if (mCustomView != null) {
mWebChromeClient?.onHideCustomView()
return
}
if (binding.youtubeWebView.canGoBack()) {
binding.youtubeWebView.goBack()
} else {
super.onBackPressed()
overridePendingTransition(R.anim.trans_left_in, R.anim.trans_right_out)
}
}
inner class YoutubeWebChromeClient() : WebChromeClient() {
private var customViewCallback: CustomViewCallback? = null
override fun onShowCustomView(view: View?, callback: CustomViewCallback?) = with(binding) {
mCustomView?.let {
callback?.onCustomViewHidden()
return@with
}
if (isAutoRotation.not()) {
this@YoutubeWebActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
}
mCustomView = view
youtubeWebView.isVisible = false // 웹뷰 숨기기
customViewContainer.isVisible = true
customViewContainer.addView(view)
customViewCallback = callback
super.onShowCustomView(view, callback)
}
@SuppressLint("SourceLockedOrientationActivity")
override fun onHideCustomView() = with(binding) {
if (mCustomView == null) {
return@with
}
youtubeWebView.isVisible = true
customViewContainer.isVisible = false
mCustomView!!.isVisible = false
customViewContainer.removeView(mCustomView)
customViewCallback?.onCustomViewHidden()
mCustomView = null
this@YoutubeWebActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
}
override fun onPause() {
super.onPause()
binding.youtubeWebView.onPause()
}
override fun onStop() {
super.onStop()
if (mCustomView != null) {
mWebChromeClient?.onHideCustomView()
}
}
}