This commit is contained in:
tateisu 2019-06-20 23:48:07 +09:00
parent 201a365401
commit c73c4da854
1 changed files with 41 additions and 27 deletions

View File

@ -47,6 +47,7 @@ import java.io.ByteArrayInputStream
import java.io.IOException
import java.util.*
import javax.net.ssl.HttpsURLConnection
import kotlin.math.max
class ActMediaViewer : AppCompatActivity(), View.OnClickListener {
@ -64,6 +65,9 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener {
internal const val EXTRA_DATA = "data"
internal const val EXTRA_SERVICE_TYPE = "serviceType"
internal const val STATE_PLAYER_POS = "playerPos"
internal const val STATE_PLAYER_PLAY_WHEN_READY = "playerPlayWhenReady"
internal fun <T : TootAttachmentLike> encodeMediaList(list : ArrayList<T>?) =
list?.encodeJson()?.toString() ?: "[]"
@ -177,6 +181,9 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener {
outState.putInt(EXTRA_IDX, idx)
outState.putInt(EXTRA_SERVICE_TYPE, serviceType.ordinal)
outState.putString(EXTRA_DATA, encodeMediaList(media_list))
outState.putLong(STATE_PLAYER_POS, exoPlayer.currentPosition)
outState.putBoolean(STATE_PLAYER_PLAY_WHEN_READY, exoPlayer.playWhenReady)
}
override fun onCreate(savedInstanceState : Bundle?) {
@ -202,14 +209,13 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener {
initUI()
load()
load(savedInstanceState)
}
override fun onDestroy() {
super.onDestroy()
pbvImage.setBitmap(null)
exoPlayer.release()
exoPlayer
}
override fun finish() {
@ -284,7 +290,7 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener {
load()
}
internal fun load() {
internal fun load(state : Bundle? = null) {
exoPlayer.stop()
pbvImage.visibility = View.GONE
@ -308,7 +314,7 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener {
TootAttachmentLike.TYPE_IMAGE -> loadBitmap(ta)
TootAttachmentLike.TYPE_VIDEO,
TootAttachmentLike.TYPE_GIFV,
TootAttachmentLike.TYPE_AUDIO ->loadVideo(ta)
TootAttachmentLike.TYPE_AUDIO -> loadVideo(ta, state)
// maybe TYPE_UNKNOWN
else -> showError(getString(R.string.media_attachment_type_error, ta.type))
}
@ -324,7 +330,7 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener {
}
@SuppressLint("StaticFieldLeak")
private fun loadVideo(ta : TootAttachment) {
private fun loadVideo(ta : TootAttachment, state : Bundle? = null) {
val url = ta.getLargeUrl(App1.pref)
if(url == null) {
@ -351,12 +357,17 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener {
mediaSource.addEventListener(App1.getAppState(this).handler, mediaSourceEventListener)
exoPlayer.prepare(mediaSource)
exoPlayer.playWhenReady = true
exoPlayer.repeatMode = when(ta.type) {
TootAttachmentLike.TYPE_VIDEO -> Player.REPEAT_MODE_OFF
// GIFV or AUDIO
else -> Player.REPEAT_MODE_ALL
}
if(state == null) {
exoPlayer.playWhenReady = true
} else {
exoPlayer.playWhenReady = state.getBoolean(STATE_PLAYER_PLAY_WHEN_READY, true)
exoPlayer.seekTo(max(0L, state.getLong(STATE_PLAYER_POS, 0L)))
}
}
private val mediaSourceEventListener = object : MediaSourceEventListener {
@ -612,7 +623,10 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener {
return bitmap2
}
fun getHttpCached(client : TootApiClient, url : String) : Pair<TootApiResult?,ByteArray?> {
fun getHttpCached(
client : TootApiClient,
url : String
) : Pair<TootApiResult?, ByteArray?> {
val result = TootApiResult.makeWithCaption(url)
val request = Request.Builder()
@ -704,7 +718,7 @@ class ActMediaViewer : AppCompatActivity(), View.OnClickListener {
val permissionCheck = ContextCompat.checkSelfPermission(
this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
if(permissionCheck != PackageManager.PERMISSION_GRANTED) {
preparePermission()