📜  如何使用YouTube API在Android中播放YouTube视频?

📅  最后修改于: 2021-05-09 17:31:28             🧑  作者: Mango

YouTube Android Player API可让您在不离开应用程序的情况下播放应用程序内的任何youtube视频或播放列表。下面提供了一个示例视频,以使您对我们在本文中将要做的事情有个大概的了解。请注意,我们将使用Kotlin语言实施此项目。

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Kotlin作为编程语言

第2步:添加YouTube Player Android API

从此处下载以添加YouTube Android播放器API 下载后,解压缩文件并将其复制到android项目的libs文件夹中。

步骤3:添加到gradle依赖关系

将以下代码添加到build.gradle(app),然后单击立即同步

步骤4.添加API密钥以播放YouTube视频

通过访问此链接添加API密钥。创建一个新项目或选择现有项目,然后创建API密钥并保存在某处。我们稍后将在我们的代码中使用它。

步骤5:添加互联网权限

转到AndroidManifest.xml并添加Internet权限。我们将需要它才能在我们的应用程序中播放视频。

步骤6:使用activity_main.xml文件

转到activity_main.xml文件,并参考以下代码。以下是activity_main.xml文件的代码。

XML


  
    
  


Kotlin
import android.os.Bundle
import android.widget.Toast
import com.google.android.youtube.player.YouTubeBaseActivity
import com.google.android.youtube.player.YouTubeInitializationResult
import com.google.android.youtube.player.YouTubePlayer
import com.google.android.youtube.player.YouTubePlayerView
  
class MainActivity : YouTubeBaseActivity() {
    // Change the AppCompactActivity to YouTubeBaseActivity()
      
    // Add the api key that you had
      // copied from google API 
    // This is a dummy api key
    val api_key =  "Enter your API key here"
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Get reference to the view of Video player
        val ytPlayer = findViewById(R.id.ytPlayer)
  
        ytPlayer.initialize(api_key, object : YouTubePlayer.OnInitializedListener{
            // Implement two methods by clicking on red error bulb
            // inside onInitializationSuccess method 
              // add the video link or the
            // playlist link that you want to play
            // In here we also handle the play and pause functionality
            override fun onInitializationSuccess(
                    provider: YouTubePlayer.Provider?,
                    player: YouTubePlayer?,
                    p2: Boolean
            ) {
                player?.loadVideo("HzeK7g8cD0Y")
                player?.play()
            }
  
            // Inside onInitializationFailure 
              // implement the failure functionality
            // Here we will show toast
            override fun onInitializationFailure(
                    p0: YouTubePlayer.Provider?,
                    p1: YouTubeInitializationResult?
            ) {
               Toast.makeText(this@MainActivity , "Video player Failed" , Toast.LENGTH_SHORT).show()
            }
        })
    }
}


步骤7:使用MainActivity.kt文件

转到MainActivity.kt文件,并参考以下代码。下面是MainActivity.kt文件的代码。在代码内部添加了注释,以更详细地了解代码。

科特林

import android.os.Bundle
import android.widget.Toast
import com.google.android.youtube.player.YouTubeBaseActivity
import com.google.android.youtube.player.YouTubeInitializationResult
import com.google.android.youtube.player.YouTubePlayer
import com.google.android.youtube.player.YouTubePlayerView
  
class MainActivity : YouTubeBaseActivity() {
    // Change the AppCompactActivity to YouTubeBaseActivity()
      
    // Add the api key that you had
      // copied from google API 
    // This is a dummy api key
    val api_key =  "Enter your API key here"
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Get reference to the view of Video player
        val ytPlayer = findViewById(R.id.ytPlayer)
  
        ytPlayer.initialize(api_key, object : YouTubePlayer.OnInitializedListener{
            // Implement two methods by clicking on red error bulb
            // inside onInitializationSuccess method 
              // add the video link or the
            // playlist link that you want to play
            // In here we also handle the play and pause functionality
            override fun onInitializationSuccess(
                    provider: YouTubePlayer.Provider?,
                    player: YouTubePlayer?,
                    p2: Boolean
            ) {
                player?.loadVideo("HzeK7g8cD0Y")
                player?.play()
            }
  
            // Inside onInitializationFailure 
              // implement the failure functionality
            // Here we will show toast
            override fun onInitializationFailure(
                    p0: YouTubePlayer.Provider?,
                    p1: YouTubeInitializationResult?
            ) {
               Toast.makeText(this@MainActivity , "Video player Failed" , Toast.LENGTH_SHORT).show()
            }
        })
    }
}

输出:

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!