视频应用程序的重要性(The Importance of Video Apps)

There’s a constant need for video-playing apps for entertainment, educational, and many other purposes. You can see it clearly if we look at Youtube usage over the past five to ten years. Even in most remote areas of the globe, mobile entertainment increased a lot. Even illiterate people in remote villages are using video apps for entertainment purposes. On the other hand, thanks to the pandemic caused by COVID-19, most schools across the world are turning to E-Learning. For these reasons, there’s a lot that can be done by one good application, built to serve the hunger of the current world. Enough talk, let’s dive into our concept: Exoplayer.

持续需要用于娱乐,教育和许多其他目的的视频播放应用程序。 如果我们查看过去五到十年的Youtube使用情况,您可以清楚地看到它。 即使在全球大多数偏远地区,移动娱乐也大大增加了。 甚至偏远乡村的文盲也都在使用视频应用来娱乐。 另一方面,由于COVID-19引起的大流行,全球大多数学校都在转向电子学习。 由于这些原因,一个好的应用程序可以完成很多工作,这些应用程序可以满足当前世界的饥饿需求。 聊够了,让我们深入探讨一下我们的概念:Exoplayer。

介绍 (Introduction)

Before the ExoPlayer Android’s MediaPlayer API is the main solution for playing audio and video both locally and over the Internet across Android devices. At the start, playing a simple video using MediaPlayer was easy but if you wanted to build a player like Youtube it would take a great deal of time, effort, and pain. My rule is to not reinvent the wheel — instead, use the perfect solution that’s already available.

在使用ExoPlayer之前,Android的MediaPlayer API是在本地以及通过Android设备通过Internet播放音频和视频的主要解决方案。 刚开始时,使用MediaPlayer播放简单的视频很容易,但是如果您想构建一个类似Youtube的播放器,将需要大量的时间,精力和痛苦。 我的原则是不要重新发明轮子,而是使用已经可用的完美解决方案。

ExoPlayer is the video player running in the Android YouTube app. It’s not part of the Android framework — it’s an open-source project maintained by Google. ExoPlayer is a library provided by Google that handles audio and video-related things on our behalf and we can call few methods to play with it. We can also make our own customization to the player UI (User Interface).

ExoPlayer是在Android YouTube应用中运行的视频播放器。 它不是Android框架的一部分,而是Google维护的一个开源项目。 ExoPlayer是Google提供的一个库,它代表我们处理与音频和视频有关的事情,我们可以调用几种方法来玩它。 我们还可以对播放器UI(用户界面)进行自定义。

ExoPlayer was built on the top of Android’s MediaCodec API, which was out in Android 4.1 (API level 16). So, if we want to integrate Exoplayer the minimum API version needs to be set to API 16 for your apps.

ExoPlayer建立在Android的MediaCodec API的顶部,而后者在Android 4.1(API级别16)中已经发布。 因此,如果我们要集成Exoplayer,则需要为您的应用将最低API版本设置为API 16。

为什么要使用ExoPlayer? (Why use ExoPlayer?)

  • ExoPlayer supports features like DASH (Dynamic Adaptive Streaming over HTTP), HLS(HTTP Live Streaming), SmoothStreaming, and Common Encryption, which are not supported by MediaPlayer.

    ExoPlayer支持DASH(HTTP上的动态自适应流),HLS(HTTP实时流),SmoothStreaming和Common Encryption等功能,而MediaPlayer不支持这些功能。

  • ExoPlayer has a modular design so it’s easily extended and customized.ExoPlayer具有模块化设计,因此可以轻松扩展和定制。
  • The major Video Streaming giants on Android mostly depend on Exoplayer because of its features and flexibility. ExoPlayer is the video player running in Android YouTube, Netflix, Amazon media player for Prime, HotStar, and many other popular and robust apps.Android上的主要视频流巨人由于其功能和灵活性而大多依赖Exoplayer。 ExoPlayer是运行在Android YouTube,Netflix,Amazon Prime,HotStar和许多其他流行且功能强大的应用程序的亚马逊媒体播放器中的视频播放器。
  • Good documentation and tutorials.好的文档和教程。
  • It’s free!免费!

Enough talk, let’s get coding.

聊够了,让我们开始编码。

基本的Android ExoPlayer应用 (Basic Android ExoPlayer App)

Let’s check the integration by creating a sample app and checking the step-by-step procedure for gain a better understanding. Create a new project from your studio and follow steps below.

让我们通过创建一个示例应用程序并检查分步过程来获得更好的理解,从而检查集成。 在您的工作室中创建一个新项目,然后执行以下步骤。

If you don’t have enough time to go step-by-step, or you intend to dive in to the code directly, check out the repo link of the sample integration:

如果您没有足够的时间逐步进行操作,或者打算直接深入研究代码,请查看示例集成的回购链接:

步骤1:在应用程序级别的build.gradle文件中添加ExoPlayer依赖项 (Step 1: Add ExoPlayer dependency in the app-level build.gradle file)

implementation 'com.google.android.exoplayer:exoplayer:2.11.8'

This can be of any version you choose — I just added the latest version available at that moment. By the way, we also have the flexibility of adding only the library modules that we actually need, rather than the complete pack mentioned above.

它可以是您选择的任何版本-我刚刚添加了当时可用的最新版本。 顺便说一下,我们还可以灵活地只添加我们实际需要的库模块,而不必添加上面提到的完整包。

implementation 'com.google.android.exoplayer:exoplayer-core:2.X.X'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.X.X'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.X.X'

The available library modules are :

可用的库模块为:

  • exoplayer-core: Core functionality (mandatory).

    exoplayer-core :核心功能(强制性)。

  • exoplayer-dash: Support for DASH content.

    exoplayer-dash :支持DASH内容。

  • exoplayer-hls: Support for HLS content.

    exoplayer-hls :支持HLS内容。

  • exoplayer-smoothstreaming: Support for SmoothStreaming content.

    exoplayer-smoothstreaming :支持exoplayer-smoothstreaming内容。

  • exoplayer-ui: UI components and resources for use with ExoPlayer.

    exoplayer-ui :与ExoPlayer一起使用的UI组件和资源。

In this example, let’s follow by adding a complete pack because adding individual modules has different requirements and can sometimes cause errors.

在此示例中,让我们接着添加一个完整的包,因为添加单个模块有不同的要求,有时会导致错误。

Also, make sure you’ve added JCenter and Google in the repositories section of your project level build.gradle file, although they’re added by default in the newer version.

另外,请确保已在repositories添加了JCenter和Google 项目级别的build.gradle文件中的“部分”,尽管它们是默认情况下在较新版本中添加的。

repositories {google()    jcenter()}

步骤2:在应用程序级别build.gradle启用Java8支持 (Step 2: Enable Java8 support in the app level build.gradle)

compileOptions {targetCompatibility JavaVersion.VERSION_1_8}

步骤3:创建XML并将ExPlayer View添加到其中 (Step 3: Create an XML and add ExPlayer View to it)

To customize the UI to look like Youtube or something else we need to add some layouts, drawables, etc, which we will see in my upcoming articles. Here, let’s inflate the basic PlayerView:

要自定义用户界面以使其看起来类似于Youtube或其他内容,我们需要添加一些布局,可绘制对象等,我们将在我的后续文章中看到它们。 在这里,让我们为基本的PlayerView充气:

<?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"><com.google.android.exoplayer2.ui.PlayerViewandroid:id="@+id/video_player_view"android:layout_width="match_parent"android:layout_height="match_parent"/></androidx.constraintlayout.widget.ConstraintLayout>

步骤4:通过创建ExoPlayer实例并分配要播放的源来初始化播放器(Step 4: Initialize the Player by Creating an Instance of ExoPlayer and Assigning a Source to Play)

As we are done with the UI, let’s create the instance of ExoPlayer to start playing the media. We can use the available builder pattern to create the instance. In most cases using SimpleExoPlayer would be a convenient way to do it — the implementation of the ExoPlayer interface.

完成UI的操作后,让我们创建ExoPlayer的实例以开始播放媒体。 我们可以使用可用的构建器模式来创建实例。 在大多数情况下,使用SimpleExoPlayer将是一种方便的方法ExoPlayer接口的实现。

val videoPlayer = SimpleExoPlayer.Builder(this).build()

As we’ve created the instance of ExoPlayer, we now need to assign it to the view created in XML:

创建ExoPlayer实例后,现在需要将其分配给以XML创建的视图:

video_player_view?.player = videoPlayer

Next, we need to build the source to play. There are many types of MediaSource’s available — let's start with ProgressiveMediaSource. Here let's take a URL and play it by building ProgressiveMediaSource.

接下来,我们需要构建播放源。 有多种类型的MediaSource可用-让我们从ProgressiveMediaSource开始 在这里,让我们采用一个URL并通过构建ProgressiveMediaSource播放它

This is generally done in two steps. In the first step we create the instance of DefaultDataSourceFactory and then pass this instance to the ProgressiveMediaSource.Factory, as below:

这通常分两个步骤完成。 在第一步中,我们创建DefaultDataSourceFactory的实例 然后将此实例传递给ProgressiveMediaSource.Factory ,如下所示:

private fun buildMediaSource(): MediaSource? {val dataSourceFactory = DefaultDataSourceFactory(this, "sample")return ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(sampleUrl))}

sampleUrl is a string variable that contains the streaming URL.

sampleUrl是包含流URL的字符串变量。

By now the initialize player method will look like this:

现在,初始化播放器方法将如下所示:

private fun initializePlayer() {videoPlayer = SimpleExoPlayer.Builder(this).build()video_player_view?.player = videoPlayerbuildMediaSource()?.let {videoPlayer?.prepare(it)}}

步骤5:添加与活动生命周期相关的基本暂停和播放功能(Step 5: Add Basic Pause and Play Functionality Relating to the Activities Lifecycle)

When we’re done, don’t forget to release the player:

完成后,请不要忘记释放播放器:

override fun onResume() {super.onResume()videoPlayer?.playWhenReady = true}override fun onStop() {super.onStop()videoPlayer?.playWhenReady = falseif (isFinishing) {releasePlayer()}}private fun releasePlayer() {videoPlayer?.release()}

步骤6:将Internet权限添加到清单文件并运行应用程序(Step 6: Add the Internet Permission to the Manifest File and Run the App)

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

You should see the video playing below with all the player functionalities: Pause, play, rewind, and seek options, etc.

您应该在下面看到具有播放器所有功能的视频播放:暂停,播放,快退和查找选项等。

The video was trimmed due to larger size
由于尺寸较大,视频被剪裁

The whole activity will look something like this:

整个活动将如下所示:

package com.sample.exoplayerimport android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() {private var videoPlayer: SimpleExoPlayer? = nullprivate var sampleUrl = "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4"override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)initializePlayer()}private fun buildMediaSource(): MediaSource? {val dataSourceFactory = DefaultDataSourceFactory(this, "sample")return ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(sampleUrl))}private fun initializePlayer() {videoPlayer = SimpleExoPlayer.Builder(this).build()video_player_view?.player = videoPlayerbuildMediaSource()?.let {videoPlayer?.prepare(it)}}override fun onResume() {super.onResume()videoPlayer?.playWhenReady = true}override fun onStop() {super.onStop()videoPlayer?.playWhenReady = falseif (isFinishing) {releasePlayer()}}private fun releasePlayer() {videoPlayer?.release()}
}

This player would work with functionality similar to YouTube. When the app goes to the background the player pauses playing, when the user comes back it plays from where it had stopped.

该播放器可以使用类似于YouTube的功能。 当应用程序进入后台时,播放器会暂停播放,而当用户返回时,它将从停止位置播放。

But in real-world scenarios, we need to handle many more things for a better user experience, like state handling of the player or holding the seek position to resume.

但是在现实世界中,我们需要处理更多事情以获得更好的用户体验,例如玩家的状态处理或保持寻找位置以恢复。

Here’s the repo link of GitHub for the sample used in this article:

这是本文中使用的示例的GitHub的repo链接:

概要 (Summary)

That’s all, for now. I hope you learned something new. Let’s check the advanced state handling, Live stream handling, beautification of UI similar to Youtube, etc in my upcoming articles. Stay tuned!

目前为止就这样了。 希望您学到了新东西。 在我即将发表的文章中,让我们检查高级状态处理,实时流处理,类似于Youtube的UI美化等。 敬请关注!

Please leave me your suggestions and comments.

请给我您的建议和意见。

Thanks for reading.

谢谢阅读。

翻译自: https://medium.com/better-programming/android-exoplayer-play-videos-in-your-app-like-youtube-486853913397


http://www.taodudu.cc/news/show-4849613.html

相关文章:

  • 每日新闻摘要:YouTube备受关注
  • 看Youtube怎么利用深度学习做推荐
  • Elmedia Player GO for Mac中文破解版永久激活教程
  • 如何为您的在线业务设置和使用YouTube
  • 如何在做YouTube推广时快速查找网红?
  • 2022游戏出海:YouTube营销推广实用攻略
  • HTML 5 版的 YouTube 支持变速视频播放
  • 建立自定义的YouTube播放列表播放器
  • Compressor mac 中文破解版永久激活教程
  • ubuntu16.04安装使用YouTube-DL和Youtube-dl GUI
  • 开创YouTube视频基础架构新时代
  • 邀请注册html模板,邀请注册页.html
  • jpg格式电脑怎么弄_电脑上怎么压缩JPG图片?两种方法帮你轻松解决
  • 【软考】加密技术
  • T3软加密注册提示“获取激活参数失败,不能连接外网,请检查网络服务器网络状态”
  • Centos8安装软加密狗
  • 金蝶软加密和硬加密
  • 软加密和硬加密
  • 软加密 vs. 硬加密
  • 软加密网上激活解决方案
  • 加密狗,由USB硬件加密狗–软加密狗(软锁)–云授权的转变
  • JAVA 软加密之加密和校验过程图
  • 软加密真的安全吗?
  • 硬加密和软加密
  • 免费的pr模板以及素材分享
  • 自学PR剪辑03
  • 自学PR剪辑02
  • 自学PR剪辑01
  • 【Pr剪辑】Pr下载链接,基础操作,渲染1080视频,视频导出,音频导出,视频变速和合并
  • 自学PR剪辑04

android exoplayer在您的应用中播放视频,例如youtube相关推荐

  1. android listview中播放视频,支持全屏

    最近在做项目的时候有需求要在 listView 中播放视频,并且支持横竖屏无缝切换,在网上搜索了一下,关于这种 demo真的很少, 有的也只是实现简单的功能,无法满足项目中的需求,想着修改一下凑合用, ...

  2. Android 视频播放器 (二):使用MediaPlayer播放视频

    在 Android 视频播放器 (一):使用VideoView播放视频 我们讲了一下如何使用VideoView播放视频,了解了基本的播放器的一些知识和内容.也知道VideoView内部封装的就是Med ...

  3. RecyclerView中播放视频

    在很多播放视频的APP中都有在列表中播放视频的效果,当点击播放的时候,在列表中播放,当视频列表滑动出屏幕的时候,就在屏幕的右下角播放.大致效果如下图: 主要思路 在和RecyclerView的同级布局 ...

  4. android播放器设置中文,Android 视频播放器 (一):使用VideoView播放视频

    一.简介 作为Android开发,我们不可避免的会接触到视频播放,VideoView做为最简单的播放器,我们是不应该不会的. 下面简单介绍一下VideoView: •  VideoView是使用Med ...

  5. Android 视频播放器 (一):使用VideoView播放视频

    一.简介 作为Android开发,我们不可避免的会接触到视频播放,VideoView做为最简单的播放器,我们是不应该不会的. 下面简单介绍一下VideoView: VideoView是使用MediaP ...

  6. ue4蓝图节点手册中文_在UE4中播放视频

    简介: 在日常使用UE4做项目时,会遇到在UE4里播放视频文件的需求,在UE4中可以使用媒体框架(Media Framework)来实现这一功能.这里介绍两种简单的方法来使用这一功能,分别是在场景里播 ...

  7. 在Winform中播放视频等【DotNet,C#】

    在项目中遇到过这样的问题,就是如何在Winform中播放视频.当时考察了几种方式,第一种是直接使用Windows Media Player组件,这种最简单:第二种是利用DirectX直接在窗体或者控件 ...

  8. vue3中播放视频和m3u8后缀的视频解决办法

    vue3中播放视频和m3u8后缀的视频解决办法 ****必须使用 vue@3.2.2及以上版本 使用 这个插件 vue3-video-play 官方地址 地址链接跳转 官方文档 文档地址 npm 安装 ...

  9. ckplayer支持html5播放吗,移动端(H5)环境中播放视频需要了解的内容-ckplayer使用方法...

    移动端(H5)环境中播放视频需要了解的内容 虽然HTML5(下面简称:H5)的技术已整体趋于成熟,但在视频播放方面,如果跟pc端的flashplayer对比的话,功能就显的非常简单了.如果你决定要让你 ...

最新文章

  1. 比特率 波特率 数据传输速率 区别(转)
  2. 有人认为,“中文编程”是解决中国程序员编程效率的秘密武器,请问它是一个“银弹”么?...
  3. .net core使用数据库
  4. 程序的加载和执行(三)——《x86汇编语言:从实模式到保护模式》读书笔记23
  5. 让DNN为您的手机提供内容
  6. python 元组长度_Python 学习笔记 - 元组
  7. 2015年第一届数据科学家大会
  8. 每天一算法(一)——用链表实现加减乘运算
  9. 神经网络用作分类器(附代码matlab)
  10. Jupyter Notebook使用
  11. zemax输出ies_基于ZEMAX的光学设计教程(第2版)
  12. k8s serviceAccountName填写后应用没有进行挂载问题处理
  13. vue+koa2移动电商实战
  14. 双十一数码产品哪些值得买?双十一好物产品分享
  15. 智慧城市专题视频课程 附教案
  16. 微信报错errcode大全
  17. 加速度计和陀螺仪指南(很详细的介绍)
  18. Java安全-Springboot Javaweb开发急速入门
  19. 阿里/腾讯云静默安装Oracle11G超级详细教程
  20. 【BP预测】基于猫群算法优化BP神经网络实现数据预测附matlab代码

热门文章

  1. python新手案例——商品进销存管理系统(pymysql+xlwt)(一)
  2. RASP之IAST扫描器的安装及使用
  3. Allegro pcb转AD
  4. 关于Application.DoEvents()
  5. 度量衡计算工具_中国古代度量衡领先世界的计量工具
  6. 瑞友天翼虚拟化系统7.0 瑞友天翼远程接入
  7. 让excel朗读英文单词本
  8. 阿里云域名 动态ip绑定 python方案
  9. linux 查看tdagent进程,Fluentd (td-agent) 日志收集系統
  10. TIA PORTAL S7-1200如何使用博途的仿真功能PLCSIM?