2020-07-26 21:16:06 +02:00
|
|
|
package sushi.hardcore.droidfs.file_viewers
|
|
|
|
|
2020-08-07 19:25:16 +02:00
|
|
|
import android.view.WindowManager
|
2021-09-01 19:31:25 +02:00
|
|
|
import com.google.android.exoplayer2.*
|
2021-06-07 13:23:11 +02:00
|
|
|
import com.google.android.exoplayer2.extractor.ExtractorsFactory
|
2020-09-05 10:27:42 +02:00
|
|
|
import com.google.android.exoplayer2.extractor.flac.FlacExtractor
|
2020-07-27 16:20:52 +02:00
|
|
|
import com.google.android.exoplayer2.extractor.mkv.MatroskaExtractor
|
|
|
|
import com.google.android.exoplayer2.extractor.mp3.Mp3Extractor
|
|
|
|
import com.google.android.exoplayer2.extractor.mp4.Mp4Extractor
|
|
|
|
import com.google.android.exoplayer2.extractor.ogg.OggExtractor
|
|
|
|
import com.google.android.exoplayer2.extractor.wav.WavExtractor
|
2021-03-17 21:11:14 +01:00
|
|
|
import com.google.android.exoplayer2.source.MediaSource
|
2020-07-26 21:16:06 +02:00
|
|
|
import com.google.android.exoplayer2.source.ProgressiveMediaSource
|
|
|
|
import sushi.hardcore.droidfs.ConstValues
|
|
|
|
import sushi.hardcore.droidfs.R
|
2020-07-27 16:20:52 +02:00
|
|
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder
|
2020-07-26 21:16:06 +02:00
|
|
|
|
|
|
|
abstract class MediaPlayer: FileViewerActivity() {
|
|
|
|
private lateinit var player: SimpleExoPlayer
|
|
|
|
|
|
|
|
override fun viewFile() {
|
2020-08-08 14:09:34 +02:00
|
|
|
initializePlayer()
|
2020-07-26 21:16:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract fun bindPlayer(player: SimpleExoPlayer)
|
2021-03-17 21:11:14 +01:00
|
|
|
protected open fun onPlaylistIndexChanged() {}
|
2021-04-05 12:42:35 +02:00
|
|
|
protected open fun onPlayerReady() {}
|
2020-07-26 21:16:06 +02:00
|
|
|
|
2021-03-17 21:11:14 +01:00
|
|
|
private fun createMediaSource(filePath: String): MediaSource {
|
2020-07-26 21:16:06 +02:00
|
|
|
val dataSourceFactory = GocryptfsDataSource.Factory(gocryptfsVolume, filePath)
|
2021-06-07 13:23:11 +02:00
|
|
|
return ProgressiveMediaSource.Factory(dataSourceFactory, ExtractorsFactory { arrayOf(
|
2020-07-27 16:20:52 +02:00
|
|
|
MatroskaExtractor(),
|
|
|
|
Mp4Extractor(),
|
|
|
|
Mp3Extractor(),
|
|
|
|
OggExtractor(),
|
2020-09-05 10:27:42 +02:00
|
|
|
WavExtractor(),
|
|
|
|
FlacExtractor()
|
2021-03-17 21:11:14 +01:00
|
|
|
) }).createMediaSource(MediaItem.fromUri(ConstValues.fakeUri))
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun initializePlayer(){
|
|
|
|
player = SimpleExoPlayer.Builder(this).build()
|
|
|
|
bindPlayer(player)
|
|
|
|
createPlaylist()
|
|
|
|
for (e in mappedPlaylist) {
|
|
|
|
player.addMediaSource(createMediaSource(e.fullPath))
|
|
|
|
}
|
|
|
|
player.repeatMode = Player.REPEAT_MODE_ALL
|
|
|
|
player.seekToDefaultPosition(currentPlaylistIndex)
|
2020-07-26 21:16:06 +02:00
|
|
|
player.playWhenReady = true
|
2021-06-07 13:23:11 +02:00
|
|
|
player.addListener(object : Player.Listener{
|
2021-04-05 12:42:35 +02:00
|
|
|
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
|
|
|
|
if (playbackState == Player.STATE_READY) {
|
|
|
|
onPlayerReady()
|
|
|
|
}
|
|
|
|
}
|
2021-09-01 19:31:25 +02:00
|
|
|
override fun onPlayerError(error: PlaybackException) {
|
|
|
|
ColoredAlertDialogBuilder(this@MediaPlayer)
|
|
|
|
.setTitle(R.string.error)
|
|
|
|
.setMessage(getString(R.string.playing_failed, error.errorCodeName))
|
|
|
|
.setCancelable(false)
|
|
|
|
.setPositiveButton(R.string.ok) { _, _ -> goBackToExplorer()}
|
|
|
|
.show()
|
2020-07-26 21:16:06 +02:00
|
|
|
}
|
2020-08-07 19:25:16 +02:00
|
|
|
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
|
|
|
if (isPlaying){
|
|
|
|
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
|
|
|
} else {
|
|
|
|
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
|
|
|
}
|
|
|
|
}
|
2021-03-17 21:11:14 +01:00
|
|
|
override fun onPositionDiscontinuity(reason: Int) {
|
|
|
|
if (player.currentWindowIndex != currentPlaylistIndex) {
|
|
|
|
playlistNext(player.currentWindowIndex == (currentPlaylistIndex+1)%mappedPlaylist.size)
|
|
|
|
onPlaylistIndexChanged()
|
|
|
|
}
|
|
|
|
}
|
2020-07-26 21:16:06 +02:00
|
|
|
})
|
2021-03-17 21:11:14 +01:00
|
|
|
player.prepare()
|
2020-07-26 21:16:06 +02:00
|
|
|
}
|
|
|
|
|
2021-03-17 21:11:14 +01:00
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
2020-07-26 21:16:06 +02:00
|
|
|
if (::player.isInitialized) {
|
|
|
|
player.release()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|