前言:
本文是根据ExoPlayer的HelloWorld文档翻译而来

Hello world!
Another way to get started is to work through the ExoPlayer codelab.
开始上手的另一种方式是通过ExoPlayer codelab.

For simple use cases, getting started with ExoPlayer consists of implementing the following steps:
做一些简单的使用案例,上手ExoPlayer按照下面的步骤实现

1.Add ExoPlayer as a dependency to your project.
添加ExoPlayer依赖到你的工程中
2.Create a SimpleExoPlayer instance.
创建一个SimpleExoPlayer实例对象
3.Attach the player to a view (for video output and user input).
关联播放器到视图,(视频输出,用户输入)
4.Prepare the player with a MediaSource to play.
为播放器准备一个MediaSource播放
5.Release the player when done.
结束使用时,释放播放器
These steps are described in more detail below. For a complete example, refer to PlayerActivity in the main demo app.
这些步骤在下面的描述中更加详细,一个更完整的例子在PlayerAtivity的演示版本app中

Adding ExoPlayer as a dependency
添加ExoPlayer依赖
Add repositories
添加仓库
The first step to getting started is to make sure you have the Google and JCenter repositories included in the build.gradle file in the root of your project.
开始之前的第一步要确保你有google和jcenter仓库在你的工程根目录下的build.gradle文件中引入

repositories {google()jcenter()
}

Add ExoPlayer modules
添加ExoPlayer的模块

Next add a dependency in the build.gradle file of your app module. The following will add a dependency to the full ExoPlayer library:
接下来添加一个依赖在你app模块的build.gradle文件当中。下面是一个完整的ExoPlayer哭的依赖添加

implementation ‘com.google.android.exoplayer:exoplayer:2.X.X’
where 2.X.X is your preferred version (the latest version can be found by consulting the release notes).

As an alternative to the full library, you can depend on only the library modules that you actually need. For example the following will add dependencies on the Core, DASH and UI library modules, as might be required for an app that plays DASH content:
除了依赖整个库,你也可以只依赖你需要的库。下面的这个例子将添加核心
根据你的需要选择
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 listed below. Adding a dependency to the full ExoPlayer library is equivalent to adding dependencies on all of the library modules individually.

exoplayer-core: Core functionality (required).
exoplayer-dash: Support for DASH content.
exoplayer-hls: Support for HLS content.
exoplayer-smoothstreaming: Support for SmoothStreaming content.
exoplayer-ui: UI components and resources for use with ExoPlayer.
In addition to library modules, ExoPlayer has multiple extension modules that depend on external libraries to provide additional functionality. Browse the extensions directory and their individual READMEs for details.

Turn on Java 8 support
If not enabled already, you need to turn on Java 8 support in all build.gradle files depending on ExoPlayer, by adding the following to the android section:

compileOptions {targetCompatibility JavaVersion.VERSION_1_8
}

Creating the player
创建一个播放器

You can create an ExoPlayer instance using SimpleExoPlayer.Builder or ExoPlayer.Builder. The builders provide a range of customization options for creating ExoPlayer instances. For the vast majority of use cases SimpleExoPlayer.Builder should be used. This builder returns SimpleExoPlayer, which extends ExoPlayer to add additional high level player functionality. The code below is an example of creating a SimpleExoPlayer.
您可以ExoPlayer使用SimpleExoPlayer.Builder或 创建实例ExoPlayer.Builder。这些构建器提供了一系列用于创建ExoPlayer实例的定制选项。对于绝大多数用例, SimpleExoPlayer.Builder都应使用。此构建器返回 SimpleExoPlayer,它扩展ExoPlayer为添加其他高级播放器功能。以下代码是创建的示例SimpleExoPlayer。

SimpleExoPlayer player = new SimpleExoPlayer.Builder(context).build();

A note on threading
关于线程的说明
ExoPlayer instances must be accessed from a single application thread. For the vast majority of cases this should be the application’s main thread. Using the application’s main thread is a requirement when using ExoPlayer’s UI components or the IMA extension.
The thread on which an ExoPlayer instance must be accessed can be explicitly specified by passing a Looper when creating the player. If no Looper is specified, then the Looper of the thread that the player is created on is used, or if that thread does not have a Looper, the Looper of the application’s main thread is used. In all cases the Looper of the thread from which the player must be accessed can be queried using Player.getApplicationLooper.
If you see “Player is accessed on the wrong thread” warnings, some code in your app is accessing a SimpleExoPlayer instance on the wrong thread (the logged stack trace shows you where!). This is not safe and may result in unexpected or obscure errors.
For more information about ExoPlayer’s treading model, see the “Threading model” section of the ExoPlayer Javadoc.

必须从单个应用程序线程访问ExoPlayer实例。在绝大多数情况下,这应该是应用程序的主线程。使用ExoPlayer的UI组件或IMA扩展时,必须使用应用程序的主线程。

Looper创建播放器时,可以通过传递a来显式指定必须访问ExoPlayer实例的线程。如果未Looper指定no ,则Looper使用创建播放器的线程的,或者如果该线程没有Looper,Looper则使用应用程序的主线程的。在所有情况下,Looper都可以使用来查询必须从中访问播放器的线程的线程 Player.getApplicationLooper。

如果看到“在错误的线程上访问了播放器”警告,则表明应用程序中的某些代码正在SimpleExoPlayer错误的线程上访问实例(记录的堆栈跟踪显示了您的位置!)。这是不安全的,并且可能导致意外或模糊的错误。

有关ExoPlayer的踩踏模型的更多信息,请参见ExoPlayer Javadoc的 “线程模型”部分。

Attaching the player to a view
将播放器附加到视图
The ExoPlayer library provides a PlayerView, which encapsulates a PlayerControlView, a SubtitleView, and a Surface onto which video is rendered. A PlayerView can be included in your application’s layout xml. Binding the player to the view is as simple as:
ExoPlayer库提供了一个PlayerView,其中封装了 PlayerControlView,a SubtitleView和a Surface渲染视频。一个PlayerView可以包含在应用程序的布局XML。将播放器绑定到视图很简单:

// Bind the player to the view.
playerView.setPlayer(player);

If you require fine-grained control over the player controls and the Surface onto which video is rendered, you can set the player’s target SurfaceView, TextureView, SurfaceHolder or Surface directly using SimpleExoPlayer’s setVideoSurfaceView, setVideoTextureView, setVideoSurfaceHolder and setVideoSurface methods respectively. You can also use PlayerControlView as a standalone component, or implement your own playback controls that interact directly with the player. SimpleExoPlayer’s addTextOutput method can be used to receive captions during playback.

如果您需要在播放器控制细粒度的控制和Surface 在其上渲染视频,可以设置玩家的目标SurfaceView, TextureView,SurfaceHolder或Surface直接使用SimpleExoPlayer的 setVideoSurfaceView,setVideoTextureView,setVideoSurfaceHolder和 setVideoSurface分别的方法。您还可以PlayerControlView用作独立组件,或实现自己的播放控件,这些控件直接与播放器进行交互。SimpleExoPlayer的addTextOutput方法可用于在播放期间接收字幕。

Preparing the player
准备播放器
In ExoPlayer every piece of media is represented by a MediaSource. To play a piece of media you must first create a corresponding MediaSource and then pass this object to ExoPlayer.prepare. The ExoPlayer library provides MediaSource implementations for DASH (DashMediaSource), SmoothStreaming (SsMediaSource), HLS (HlsMediaSource) and regular media files (ProgressiveMediaSource). The following code shows how to prepare the player with a MediaSource suitable for playback of an MP4 file.
在ExoPlayer中,每个媒体都由表示MediaSource。要播放某种媒体,您必须先创建一个对应的媒体MediaSource,然后将此对象传递给ExoPlayer.prepare。ExoPlayer库提供 MediaSource了DASH(DashMediaSource),SmoothStreaming(SsMediaSource),HLS(HlsMediaSource)和常规媒体文件(ProgressiveMediaSource)的实现。以下代码显示了如何准备MediaSource适合播放MP4文件的播放器。

// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context,Util.getUserAgent(context, "yourApplicationName"));
// This is the MediaSource representing the media to be played.
MediaSource videoSource =new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mp4VideoUri);
// Prepare the player with the source.
player.prepare(videoSource);

Controlling the player
控制播放器
Once the player has been prepared, playback can be controlled by calling methods on the player. For example setPlayWhenReady starts and pauses playback, the various seekTo methods seek within the media,setRepeatMode controls if and how media is looped, setShuffleModeEnabled controls playlist shuffling, and setPlaybackParameters adjusts playback speed and pitch.

If the player is bound to a PlayerView or PlayerControlView, then user interaction with these components will cause corresponding methods on the player to be invoked.
准备好播放器后,可以通过调用播放器上的方法来控制播放。例如,setPlayWhenReady开始和暂停播放,各种seekTo方法都可以在媒体中进行搜索,setRepeatMode控制是否循环媒体以及如何循环播放,setShuffleModeEnabled控制播放列表改组以及 setPlaybackParameters调整播放速度和音高。

如果播放器绑定到PlayerView或PlayerControlView,则用户与这些组件的交互将导致调用播放器上的相应方法。

Releasing the player
释放播放器
It’s important to release the player when it’s no longer needed, so as to free up limited resources such as video decoders for use by other applications. This can be done by calling ExoPlayer.release.
重要的是在不再需要播放器时将其释放,以释放有限的资源(例如视频解码器)供其他应用程序使用。这可以通过调用来完成ExoPlayer.release。

ExoPlayer尝鲜相关推荐

  1. galaxy android 8,三星终于正式升级安卓8.0!Galaxy S8尝鲜

    原标题:三星终于正式升级安卓8.0!Galaxy S8尝鲜 经过长时间的测试,在友商陆续吃上奥利奥之后,三星终于开始向Galaxy S8/S8+正式推送安卓8.0,也带来了新的Experience U ...

  2. 鸿蒙系统开发者公测,公测尝鲜开启!华为Mate40/P40开始和安卓渐行渐远

    目前华为官方发布公告,华为EMUI的微博等相关官方媒体账号全部更名为HarmonyOS,这也意味着HarmonyOS将会成为华为在软件系统层面的重心,彻底替代基于Android的华为EMUI. 至于那 ...

  3. 华为鸿蒙去哪里更新,华为鸿蒙OS正式尝鲜版名单更新,升级?还是不升级?

    原标题:华为鸿蒙OS正式尝鲜版名单更新,升级?还是不升级? 请点击[关注],获取更多数码资讯 华为鸿蒙系统正式版本已经开始推送,公测尝鲜版与正式尝鲜版机型名单已经更新:"Mate40系列.M ...

  4. K3s初探:Rancher架构师带你尝鲜史上最轻量Kubernetes发行版

    发布不到两天,GitHub上Star数已近3000,这个业界大热的.史上最轻量的开源Kubernetes发行版,你试过了没? Rancher资深架构师来教你走出尝鲜第一步!使用教程在此! 前 言 昨天 ...

  5. 特斯拉自动驾驶新能力:识别红绿灯停车标识;尝鲜车主:实用好用

    白交 发自 凹非寺  量子位 报道 | 公众号 QbitAI 特斯拉,现在可以买到的最有智能化体验的汽车. 撇开安全话题,自动驾驶的能力和功能,一直走在行业最前沿. 这不,城区道路驾驶里,连交通信号灯 ...

  6. android oppo 权限,OPPO Reno可尝鲜Android Q:教程如下

    原标题:OPPO Reno可尝鲜Android Q:教程如下 5月8日凌晨,Android Q在谷歌I/O开发者大会上正式亮相.在I/O大会现场,谷歌公布了首批Android Q升级名单,其中OPPO ...

  7. 前端每周清单第 49 期:Webpack 4 Beta 尝鲜,React Windowing 与 setState 分析

    前端每周清单专注前端领域内容,以对外文资料的搜集为主,帮助开发者了解一周前端热点:分为新闻热点.开发教程.工程实践.深度阅读.开源项目.巅峰人生等栏目.欢迎关注[前端之巅]微信公众号(ID: fron ...

  8. 尝鲜Ubuntu Server 12.04 LTS

    作为一个尝试远离Windows的Linux初学者,一直都比较关注对初学者来说比较简单的Ubuntu Server. 4月中旬之后有几次都需要装DB服务器,因为希望能够使用最新版本,所以就一直在等Ubu ...

  9. 小米10谷歌连携失败_Android 11 喜讯!小米 10 率先尝鲜,官方刷机包发布下载

    6月15日消息,小米MIUI官方今天宣布Android11Beta1来了,小米10系列手机率先尝鲜体验,MIUI官方还表示"欢迎开发者进行体验",基于Android11Beta1底 ...

最新文章

  1. js算法入门(3)--递归
  2. 取代C语言标准输入输出:cin 和 cout【C++标准输入输出】
  3. win10 创建python虚拟环境
  4. tensorflow综合示例7:LeNet-5实现mnist识别
  5. GitHub Research:超过50%的Java记录语句写错了
  6. python 文件状态_Python:如何访问文件的状态
  7. 国内专业移动广告聚合平台,KeyMob,手机广告效果最棒
  8. python数据分析第一步:读取以及查看数据
  9. 关于Mybatis的一点小记录(parameterType)
  10. GJB 软件质量保证计划(模板)
  11. 北大核心2020_“三个月不录用视为拒稿”,核心期刊投稿,编辑的这句话别有用意...
  12. eclipse SVN javaHL not available 问题解决
  13. Windows Server AppFabric正式发布
  14. 正交试验案例分析全步骤
  15. ubuntu使用CNKI官方的caj浏览器
  16. 墨者Apache Struts2远程代码执行漏洞(S2-009)复现题解
  17. gitlab之权限设置
  18. 亚马逊服务器一键重装系统,如何使用Amazon Alexa轻松设置智能家居设备
  19. SEGGER公司JlinkV9仿真器实现串口通讯VCOM和SWD调试双功能
  20. 物联网IoT应用技术有哪些?

热门文章

  1. 字节跳动前端用什么框架
  2. 以下选项中不是python ide的是_python选择题word打印版
  3. ZCMU1692-鬼吹灯
  4. 高性能计算机团队建设,什么叫团队建设 团队建设提升团队的凝聚力
  5. USB-C显示器专用的PD协议芯片,LDR6282了解学习一下
  6. 怎样使用MFC 调用windows系统程序 windows media player
  7. js练手小项目——JavaScript实现进度条
  8. Ubuntu 安装codeblock
  9. 阿里四面+蚂蚁金服四面全挂,是我技术太菜了吗?
  10. Navicat的安装与mysql数据库的连接保姆级教程