使用Kotlin构建YouTube播放器

In this article, we will learn how to integrate and play a video using YouTube API in Android with Kotlin.

在本文中,我们将学习如何在带有Kotlin的Android中使用YouTube API集成和播放视频。

The YouTube Android Player API enables you to incorporate video playback functionality into your Android applications. The API defines methods for loading and playing YouTube videos (and playlists) and for customizing and controlling the video playback experience.

YouTube Android Player API使您可以将视频播放功能合并到Android应用程序中。 该API定义了用于加载和播放YouTube视频(和播放列表)以及自定义和控制视频播放体验的方法。

Using the API, you can load or cue videos into a player view embedded in your application’s UI. You can then control playback programmatically. For example, you can play, pause, or seek to a specific point in the currently loaded video.

使用API​​,您可以将视频加载或提示到嵌入在应用程序UI中的播放器视图中。 然后,您可以通过编程方式控制播放。 例如,您可以播放,暂停或搜索当前加载的视频中的特定点。

You can also register event listeners to get callbacks for certain events, such as the player loading a video or the player state changing. Finally, the API has helper functionality to support orientation changes as well as transitions to fullscreen playback.

您还可以注册事件侦听器,以获取某些事件的回调,例如播放器正在加载视频或播放器状态更改。 最后,该API具有帮助程序功能以支持方向更改以及向全屏播放的过渡。

Before going to the coding part, we need to follow the below steps for setting up the YouTube API in our own developed app.

在进行编码之前,我们需要按照以下步骤在自己开发的应用中设置YouTube API。

创建一个Android项目: (Creating an Android Project:)

Step 1- Creating a new file project with an empty activity

第1步-创建一个活动为空的新文件项目

使用Kotlin构建YouTube播放器
  1. Go to Files

    转到文件

  2. Create a New Project

    创建一个新项目

  3. Select a Project Template- Basic Activity

    选择一个项目模板-基本活动

Step 2- Setting up YouTube Library and Manifest

第2步-设置YouTube库和清单

In this part, we will see how to set up the YouTube API Client library for the project.

在这一部分,我们将了解如何为项目设置YouTube API客户端库。

使用Kotlin构建YouTube播放器
使用Kotlin构建YouTube播放器
  1. Go to YouTube Android Player API and download YouTube Android Player API jar file — Link

    转到YouTube Android Player API并下载YouTube Android Player API jar文件— 链接

  2. Extract the jar files and add them to the “libs” folder.

    解压缩jar文件并将其添加到“ libs”文件夹中。

使用Kotlin在屏幕上集成YouTube Android Player (Integration of YouTube Android Player in Screen using Kotlin)

Step 1: Go to Google Developer Console — https://console.developers.google.com/apis/library/

第1步:转到Google开发者控制台-https: //console.developers.google.com/apis/library/

Step 2: Enable YouTube Data API

步骤2:启用YouTube数据API

使用Kotlin构建YouTube播放器

Step 3: Go to “Help me choose” option

第3步:转到“帮助我选择”选项

使用Kotlin构建YouTube播放器

Step 4: Select the credentials as shown below

步骤4:选择凭据,如下所示

使用Kotlin构建YouTube播放器

Step 5: Go to Credentials

第5步:转到凭证

使用Kotlin构建YouTube播放器
使用Kotlin构建YouTube播放器
使用Kotlin构建YouTube播放器
使用Kotlin构建YouTube播放器
使用Kotlin构建YouTube播放器

1. In the place of “Name” enter your package name

1.在 “名称” 的地方 输入您的包裹名称

2. Generate the SHA-1 certificate fingerprint by running “signingReport”

2. 通过运行 “ signingReport” 生成 SHA-1证书指纹

3. Copy the generated SHA-1 certificate fingerprint and place it as shown above

3. 复制生成的 SHA-1证书指纹 并将其放置,如图所示

完整代码: (Full Code:)

Now, you can run the code and see the video on the emulator.

现在,您可以运行代码并在模拟器上观看视频。

使用Kotlin构建YouTube播放器

Link to my Github Project- https://github.com/mitushaa/YouTube-Player-API

链接到我的Github项目-https ://github.com/mitushaa/YouTube-Player-API

Thanks for reading :)

谢谢阅读 :)

翻译自: https://blog.kotlin-academy.com/building-a-youtube-player-using-kotlin-b0e4beef302a