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)
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 (