package com.dreamteam.timelapse

import android.media.MediaPlayer

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.ui.PlayerView
class VideoPlayerActivity : AppCompatActivity() {
    private lateinit var player: ExoPlayer
    private lateinit var playerView: PlayerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_video_player)

        // Get reference to the PlayerView
        playerView = findViewById(R.id.playerView)

        // Initialize the player
        player = ExoPlayer.Builder(this).build()

        // Get video URL from intent
        val videoUrl = intent.getStringExtra("VIDEO_URL")
        if (videoUrl.isNullOrEmpty()) {
            Toast.makeText(this, "Invalid video URL", Toast.LENGTH_SHORT).show()
            return
        }

        // Create a MediaItem from the URL
        val mediaItem = MediaItem.fromUri(videoUrl)

        // Set the media item to the player
        player.setMediaItem(mediaItem)

        // Prepare and start playback
        player.prepare()
        player.play()

        // Connect the player to the PlayerView
        playerView.player = player
    }

    override fun onPause() {
        super.onPause()
        player.pause() // Pause the player when the activity is paused
    }

    override fun onStop() {
        super.onStop()
        player.release() // Release the player when the activity is stopped
    }
}