From eeedf742a6d4a0433df171ce0998a1bc5471f1a2 Mon Sep 17 00:00:00 2001 From: tateisu Date: Tue, 26 Jul 2022 10:49:56 +0900 Subject: [PATCH] =?UTF-8?q?=E5=86=85=E8=94=B5=E3=83=93=E3=83=A5=E3=83=BC?= =?UTF-8?q?=E3=82=A2=E3=81=A7remote=5Furl=E3=81=AE=E5=8B=95=E7=94=BB?= =?UTF-8?q?=E5=86=8D=E7=94=9F=E3=81=AB=E5=A4=B1=E6=95=97=E3=81=97=E3=81=9F?= =?UTF-8?q?=E3=82=89=E8=87=AA=E3=82=B5=E3=83=BC=E3=83=90=E3=81=AEurl?= =?UTF-8?q?=E3=81=A7=E5=86=8D=E7=94=9F=E3=81=97=E3=81=AA=E3=81=8A=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jp/juggler/subwaytooter/ActMediaViewer.kt | 40 ++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/jp/juggler/subwaytooter/ActMediaViewer.kt b/app/src/main/java/jp/juggler/subwaytooter/ActMediaViewer.kt index 859f0514..d3ffed11 100644 --- a/app/src/main/java/jp/juggler/subwaytooter/ActMediaViewer.kt +++ b/app/src/main/java/jp/juggler/subwaytooter/ActMediaViewer.kt @@ -39,6 +39,7 @@ import jp.juggler.subwaytooter.pref.put import jp.juggler.subwaytooter.util.ProgressResponseBody import jp.juggler.subwaytooter.view.PinchBitmapView import jp.juggler.util.* +import kotlinx.coroutines.yield import okhttp3.Request import java.io.ByteArrayInputStream import java.io.IOException @@ -112,6 +113,8 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener { internal var bufferingLastShown: Long = 0 + private var lastVideoUrl: String? = null + private val tileStep by lazy { val density = resources.displayMetrics.density (density * 12f + 0.5f).toInt() @@ -158,8 +161,9 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener { } override fun onPlayerError(error: PlaybackException) { - log.d("exoPlayer onPlayerError") - showToast(error, "player error.") + log.w(error, "exoPlayer onPlayerError") + if (recoverLocalVideo()) return + showToast(error, "exoPlayer onPlayerError") } override fun onPositionDiscontinuity( @@ -421,8 +425,11 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener { } } - @SuppressLint("StaticFieldLeak") - private fun loadVideo(ta: TootAttachment, state: Bundle? = null) { + private fun loadVideo( + ta: TootAttachment, + state: Bundle? = null, + forceLocalUrl: Boolean = false, + ) { views.cbMute.visible().run { if (isChecked && lastVolume.isFinite()) { @@ -430,7 +437,10 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener { } } - val url = ta.getLargeUrl(appPref) + val url = when { + forceLocalUrl -> ta.url + else -> ta.getLargeUrl(appPref) + } if (url == null) { showError("missing media attachment url.") return @@ -440,6 +450,7 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener { showError("can't parse URI: $url") return } + lastVideoUrl = url // https://github.com/google/ExoPlayer/issues/1819 HttpsURLConnection.setDefaultSSLSocketFactory(MySslSocketFactory) @@ -864,4 +875,23 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener { } ad.show(this, getString(R.string.background_pattern)) } + + /** + * remote_urlを再生できなかった場合、自サーバで再生し直す + */ + private fun recoverLocalVideo(): Boolean { + val ta = mediaList.elementAtOrNull(idx) + if (ta != null && + lastVideoUrl == ta.remote_url && + !ta.url.isNullOrEmpty() && + ta.url != ta.remote_url + ) { + launchMain { + yield() + loadVideo(ta, forceLocalUrl = true) + } + return true + } + return false + } }