Audio and fullscreen video

This commit is contained in:
Matthieu 2022-02-17 12:08:57 +01:00
parent 03f83790f2
commit 77c895bbf2
2 changed files with 53 additions and 1 deletions

View File

@ -25,6 +25,7 @@
<activity
android:name=".posts.MediaViewerActivity"
android:exported="false"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".postCreation.camera.CameraActivity" />
<activity

View File

@ -2,8 +2,14 @@ package org.pixeldroid.app.posts
import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.media.AudioManager.STREAM_MUSIC
import android.os.Bundle
import androidx.core.net.toUri
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.media.AudioAttributesCompat
import androidx.media2.common.MediaMetadata
import androidx.media2.common.UriMediaItem
import androidx.media2.player.MediaPlayer
@ -12,6 +18,7 @@ import org.pixeldroid.app.utils.BaseActivity
class MediaViewerActivity : BaseActivity() {
private lateinit var mediaPlayer: MediaPlayer
private lateinit var binding: ActivityMediaviewerBinding
companion object {
@ -39,8 +46,42 @@ class MediaViewerActivity : BaseActivity() {
.putString(MediaMetadata.METADATA_KEY_TITLE, description ?: "")
.build()
val mediaPlayer = MediaPlayer(this)
mediaPlayer = MediaPlayer(this)
mediaPlayer.setMediaItem(mediaItem)
binding.videoView.mediaControlView?.setOnFullScreenListener{ view, fullscreen ->
val windowInsetsController = ViewCompat.getWindowInsetsController(window.decorView)
if (!fullscreen) {
// Configure the behavior of the hidden system bars
windowInsetsController?.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
// Hide both the status bar and the navigation bar
windowInsetsController?.show(WindowInsetsCompat.Type.systemBars())
supportActionBar?.show()
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
} else {
// Configure the behavior of the hidden system bars
windowInsetsController?.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
// Hide both the status bar and the navigation bar
windowInsetsController?.hide(WindowInsetsCompat.Type.systemBars())
requestedOrientation =
if (mediaPlayer.videoSize.height < mediaPlayer.videoSize.width) {
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
} else {
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
supportActionBar?.hide()
}
}
// Configure audio
mediaPlayer.setAudioAttributes(AudioAttributesCompat.Builder()
.setLegacyStreamType(STREAM_MUSIC)
.setUsage(AudioAttributesCompat.USAGE_MEDIA)
.setContentType(AudioAttributesCompat.CONTENT_TYPE_MOVIE)
.build()
)
mediaPlayer.prepare()
binding.videoView.setPlayer(mediaPlayer)
@ -48,4 +89,14 @@ class MediaViewerActivity : BaseActivity() {
// Start actually playing the video
mediaPlayer.play()
}
override fun onPause() {
super.onPause()
mediaPlayer.pause()
}
override fun onDestroy() {
super.onDestroy()
mediaPlayer.close()
}
}