Open In App

How to Create Video Player and Downloader App in Android?

Last Updated : 23 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will walk you through the process of creating a simple video player and downloader app in Android using Kotlin and Android Studio. In this project, we will use firebase to store videos and generate URLs. A sample video is given below to get an idea about what we are going to do in this article.

Step-by-Step Implementation

Step 1: Creating a new project 

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Add Internet permission  in the AndroidManifest.xml file

XML




<uses-permission android:name="android.permission.INTERNET"/>


Step 3: Designing the user interface

Now that we have created our project, it’s time to design the user interface. The user interface is what the user will see and interact with, so it’s important to get it right.

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <Button
        android:id="@+id/download"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/video_view"
        android:layout_margin="15dp"
        android:text="Download Now">
    </Button>
  
</androidx.constraintlayout.widget.ConstraintLayout>


Step 4: Adding functionality to the app

Once we have designed the user interface, it’s time to add functionality to the app. To do this, we will write code in Kotlin. To play videos in the app, we will use the VideoView class in Android. This class provides a simple way to play videos in an Android app. To download videos, we will use the DownloadManager class in Android, which provides a simple way to download files from the internet.

Kotlin




package com.sangyn.videodownloader
  
import android.app.DownloadManager
import android.content.Context
import android.widget.MediaController
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import android.widget.VideoView
  
// MainActivity class extends AppCompatActivity
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
         
        // Set the content view for the activity
        setContentView(R.layout.activity_main)
          
        // URL of the video to be played
          
        // Get the VideoView and MediaController objects
        val videoView = findViewById<VideoView>(R.id.video_view)
        val mediaController = MediaController(this)
          
        // Set the video URI for the VideoView
        videoView.setVideoURI(Uri.parse(url))
          
        // Set the MediaController for the VideoView
        videoView.setMediaController(mediaController)
          
        // Set the anchor view for the MediaController
        mediaController.setAnchorView(videoView)
          
        // Start playing the video
        videoView.start()
          
        // Get the download button
        val downloadButton = findViewById<Button>(R.id.download)
          
          // Set the onClickListener for the download button
        downloadButton.setOnClickListener {
            // Get the DownloadManager
            val download = applicationContext.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
              
            // Get the URI of the video
            val videoUri = Uri.parse(url)
              
            // Create a DownloadManager request for the video
            val getVideo = DownloadManager.Request(videoUri)
              
            // Set the visibility of the download notification
            getVideo.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
              
            // Enqueue the request in the DownloadManager
            download.enqueue(getVideo)
              
            // Show a Toast message indicating that the download has started
            Toast.makeText(this, "Download Started", Toast.LENGTH_LONG).show()
        }
    }
}


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads