Share
Explore

Simple Android Kotlin app that demonstrates a YouTube video player using the YouTube Android Player API.

Hand in Format:
Make a Word Document named as StudentName_StudentID.docx
Put all code
Screen shots of Android Studio
Screen shots of App working in ADV / on physical device.

This app will be compatible with Android Studio Iguana | 2023.2.1 Patch 2 and will use Kotlin DSL for Gradle files.
First, you'll need to set up a YouTube API key:
1. Go to the Google Developers Console (https://console.developers.google.com/)
2. Create a new project or select an existing one
3. Enable the YouTube Data API v3
image.png
image.png

4. Create credentials (API key) for your project
image.png
image.png

image.png

Now, let's create the app:

1. MainActivity.kt
```kotlin package com.example.youtubedemo
import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import com.google.android.youtube.player.YouTubeStandalonePlayer import com.example.youtubedemo.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding private val API_KEY = "YOUR_API_KEY_HERE" private val VIDEO_ID = "dQw4w9WgXcQ" // Example: Rick Astley - Never Gonna Give You Up
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root)
binding.btnPlayVideo.setOnClickListener { val intent = YouTubeStandalonePlayer.createVideoIntent(this, API_KEY, VIDEO_ID) startActivity(intent) } } } ```
2. build.gradle.kts (Project level)
```kotlin plugins { id("com.android.application") version "8.2.2" apply false id("org.jetbrains.kotlin.android") version "1.9.22" apply false } ```
3. build.gradle.kts (Module level)
```kotlin plugins { id("com.android.application") id("org.jetbrains.kotlin.android") }
android { namespace = "com.example.youtubedemo" compileSdk = 34
defaultConfig { applicationId = "com.example.youtubedemo" minSdk = 24 targetSdk = 34 versionCode = 1 versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" }
buildTypes { release { isMinifyEnabled = false proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = "1.8" } buildFeatures { viewBinding = true } }
dependencies { implementation("androidx.core:core-ktx:1.12.0") implementation("androidx.appcompat:appcompat:1.6.1") implementation("com.google.android.material:material:1.11.0") implementation("androidx.constraintlayout:constraintlayout:2.1.4") implementation("com.google.apis:google-api-services-youtube:v3-rev20230629-2.0.0") implementation("com.google.android.youtube:youtube-android-player-api:1.0.0") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") } ```
4. activity_main.xml
```xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<Button android:id="@+id/btnPlayVideo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Play YouTube Video" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> ```
5. AndroidManifest.xml
```xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.YouTubeDemo" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
</manifest> ```

Instructions to set up and run the app:

1. Create a new Android project in Android Studio named "YouTubeDemo" with the package name "com.example.youtubedemo".
2. Replace the contents of MainActivity.kt, build.gradle.kts (Project level), build.gradle.kts (Module level), activity_main.xml, and AndroidManifest.xml with the provided code.
3. In MainActivity.kt, replace "YOUR_API_KEY_HERE" with your actual YouTube API key.
4. Download the YouTube Android Player API JAR file from https://developers.google.com/youtube/android/player/downloads
5. Create a new directory in your project: app/libs 6. Copy the downloaded JAR file (YouTubeAndroidPlayerApi.jar) into the app/libs directory. 7. Sync the project with Gradle files. 8. Run the app on an emulator or physical device.
This simple app demonstrates a YouTube video player by:
1. Setting up the necessary dependencies for the YouTube Android Player API.
2. Creating a button that, when clicked, opens a YouTube video in the YouTube Standalone Player.
3. Using a hardcoded YouTube video ID in the MainActivity.

When students run this app, they will see a button on the screen.
Clicking the button will open the YouTube Standalone Player and play the specified video (in this case, Rick Astley's "Never Gonna Give You Up").
The app demonstrates how to integrate YouTube video playback into an Android application using the official YouTube Android Player API.
Remember to explain to students the importance of keeping API keys secure and not committing them directly to version control. In a real-world application, you would want to store the API key more securely.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.