faqs.md 9.8 KB
Newer Older
O
ojw28 已提交
1 2 3
---
layout: default
title: FAQs
O
Oliver Woodman 已提交
4
weight: 5
O
ojw28 已提交
5 6
---

O
ojw28 已提交
7
* [What formats does ExoPlayer support?][]
O
Oliver Woodman 已提交
8
* [Why are some media files not seekable?][]
O
Oliver Woodman 已提交
9 10 11
* [Why do some MPEG-TS files fail to play?][]
* [Why do some streams fail with HTTP response code 301 or 302?][]
* [How can I query whether the stream being played is a live stream?][]
O
Oliver Woodman 已提交
12
* [How do I keep audio playing when my app is backgrounded?][]
O
ojw28 已提交
13
* [How do I get smooth animation/scrolling of video?][]
O
ojw28 已提交
14
* [Should I use SurfaceView or TextureView?][]
O
Oliver Woodman 已提交
15
* [Does ExoPlayer support emulators?][]
O
ojw28 已提交
16

O
ojw28 已提交
17 18
---

O
ojw28 已提交
19 20
#### What formats does ExoPlayer support? ####

O
ojw28 已提交
21
See the [Supported formats][] page.
O
ojw28 已提交
22

O
Oliver Woodman 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36
#### Why are some media files not seekable? ####

ExoPlayer does not support seeking in media where the only method for performing
accurate seek operations is for the player to scan and index the entire file.
ExoPlayer considers such files as unseekable. Most modern media container
formats include metadata for seeking (e.g., a sample index), have a well defined
seek algorithm (e.g., interpolated bisection search for Ogg), or indicate that
their content is constant bitrate. Efficient seek operations are possible and
supported by ExoPlayer in these cases.

If you require seeking but have unseekable media, we suggest converting your
content to use a more appropriate container format. In the specific case of
unseekable MP3 files, you can enable seeking under the assumption that the
files have a constant bitrate using [FLAG_ENABLE_CONSTANT_BITRATE_SEEKING][].
O
Oliver Woodman 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
This can be set on a DefaultExtractorsFactory using [setMp3ExtractorFlags][].

#### Why do some MPEG-TS files fail to play? ####

Some MPEG-TS files do not contain access unit delimiters (AUDs). By default
ExoPlayer relies on AUDs to cheaply detect frame boundaries. Similarly, some
MPEG-TS files do not contain IDR keyframes. By default these are the only type
of keyframes considered by ExoPlayer.

ExoPlayer will appear to be stuck in the buffering state when asked to play an
MPEG-TS file that lacks AUDs or IDR keyframes. If you need to play such files,
you can do so using [FLAG_DETECT_ACCESS_UNITS][] and
[FLAG_ALLOW_NON_IDR_KEYFRAMES][] respectively. These flags can be set on a
DefaultExtractorsFactory using [setTsExtractorFlags][]. Use of
FLAG_DETECT_ACCESS_UNITS has no side effects other than being computationally
expensive relative to AUD based frame boundary detection. Use of
FLAG_ALLOW_NON_IDR_KEYFRAMES may result in temporary visual corruption at the
start of playback and immediately after seeks when playing some MPEG-TS files.

#### Why do some streams fail with HTTP response code 301 or 302? ####

HTTP response codes 301 and 302 both indicate redirection. Brief descriptions
can be found on [Wikipedia][]. When ExoPlayer makes a request and receives a
response with status code 301 or 302, it will normally follow the redirect
and start playback as normal. The one case where this does not happen by default
is for cross-protocol redirects. A cross-protocol redirect is one that redirects
from HTTPS to HTTP or vice-versa (or less commonly, between another pair of
protocols). You can test whether a URL causes a cross-protocol redirect using
the [wget][] command line tool as follows:
```wget "https://yourserver.com/test.mp3" 2>&1  | grep Location
```
The output should look something like this:
```$ wget "https://yourserver.com/test.mp3" 2>&1  | grep Location
Location: https://second.com/test.mp3 [following]
Location: http://third.com/test.mp3 [following]
```
In this example there are two redirects. The first redirect is from
`https://yourserver.com/test.mp3` to `https://second.com/test.mp3`. Both are
HTTPS, and so this is not a cross-protocol redirect. The second redirect is from
`https://second.com/test.mp3` to `http://third.com/test.mp3`. This redirects
from HTTPS to HTTP and so is a cross-protocol redirect. ExoPlayer will not
follow this redirect in its default configuration, meaning playback will fail.

If you need to, you can configure ExoPlayer to follow cross-protocol redirects
when instantiating the `HttpDataSource.Factory` instances used by ExoPlayer in
your application. [`DefaultHttpDataSourceFactory`][] has constructors that
accept an `allowCrossProtocolRedirects` argument for this purpose, as do other
`HttpDataSource.Factory` implementations. Set these arguments to true to enable
cross-protocol redirects.

#### How can I query whether the stream being played is a live stream? ####

You can query ExoPlayer's [isCurrentWindowDynamic][] method. A dynamic window
implies that the stream being played is a live stream.
O
Oliver Woodman 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104

#### How do I keep audio playing when my app is backgrounded? ####

There are a few steps that you need to take to ensure continued playback of
audio when your app is in the background:

1. You need to have a running [foreground service][]. This prevents the system
   from killing your process to free up resources.
1. You need to hold a [WifiLock][] and a [WakeLock][]. These ensure that the
   system keeps the WiFi radio and CPU awake.

It's important that you stop the service and release the locks as soon as audio
is no longer being played.

O
ojw28 已提交
105 106
#### How do I get smooth animation/scrolling of video? ####

O
Oliver Woodman 已提交
107 108 109 110 111 112
`SurfaceView` rendering wasn't properly synchronized with view animations until
Android N. On earlier releases this could result in unwanted effects when a
`SurfaceView` was placed into scrolling container, or when it was subjected to
animation. Such effects included the `SurfaceView`'s contents appearing to lag
slightly behind where it should be displayed, and the view turning black when
subjected to animation.
O
ojw28 已提交
113

O
Oliver Woodman 已提交
114 115 116 117
To achieve smooth animation or scrolling of video prior to Android N, it's
therefore necessary to use `TextureView` rather than `SurfaceView`. If smooth
animation or scrolling is not required then `SurfaceView` should be preferred
(see [Should I use SurfaceView or TextureView?][]).
O
ojw28 已提交
118 119 120 121 122 123 124 125 126

#### Should I use SurfaceView or TextureView? ####

`SurfaceView` has a number of benefits over `TextureView` for video playback:

* Significantly lower power consumption on many devices.
* More accurate frame timing, resulting in smoother video playback.
* Support for secure output when playing DRM protected content.

O
Oliver Woodman 已提交
127 128 129 130 131 132
`SurfaceView` should therefore be preferred over `TextureView` where possible.
`TextureView` should be used only if `SurfaceView` does not meet your needs. One
example is where smooth animations or scrolling of the video surface is required
prior to Android N (see [How do I get smooth animation/scrolling of video?][]).
For this case, it's preferable to use `TextureView` only when [`SDK_INT`][] is
less than 24 (Android N) and `SurfaceView` otherwise.
O
ojw28 已提交
133

O
Oliver Woodman 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147
#### Does ExoPlayer support emulators? ####

If you're seeing ExoPlayer fail when using an emulator, this is usually because
the emulator does not properly implement components of Android's media stack.
This is an issue with the emulator, not with ExoPlayer. Android's official
emulator ("Virtual Devices" in Android Studio) supports ExoPlayer provided the
system image has an API level of at least 23. System images with earlier API
levels do not support ExoPlayer. The level of support provided by third party
emulators varies. If you find a third party emulator on which ExoPlayer fails,
you should report this to the developer of the emulator rather than to the
ExoPlayer team. Where possible, we recommend testing media applications on
physical devices rather than emulators.

[What formats does ExoPlayer support?]: #what-formats-does-exoplayer-support
O
Oliver Woodman 已提交
148
[Why are some media files not seekable?]: #why-are-some-media-files-not-seekable
O
Oliver Woodman 已提交
149 150 151
[Why do some MPEG-TS files fail to play?]: #why-do-some-mpeg-ts-files-fail-to-play
[Why do some streams fail with HTTP response code 301 or 302?]: #why-do-some-streams-fail-with-http-response-code-301-or-302
[How can I query whether the stream being played is a live stream?]: #how-can-i-query-whether-the-stream-being-played-is-a-live-stream
O
Oliver Woodman 已提交
152
[How do I keep audio playing when my app is backgrounded?]: #how-do-i-keep-audio-playing-when-my-app-is-backgrounded
O
Oliver Woodman 已提交
153 154 155
[How do I get smooth animation/scrolling of video?]: #how-do-i-get-smooth-animationscrolling-of-video
[Should I use SurfaceView or TextureView?]: #should-i-use-surfaceview-or-textureview
[Does ExoPlayer support emulators?]: #does-exoplayer-support-emulators
O
Oliver Woodman 已提交
156

O
ojw28 已提交
157
[Supported formats]: https://google.github.io/ExoPlayer/supported-formats.html
O
Oliver Woodman 已提交
158
[FLAG_ENABLE_CONSTANT_BITRATE_SEEKING]: https://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/extractor/mp3/Mp3Extractor.html#FLAG_ENABLE_CONSTANT_BITRATE_SEEKING
O
Oliver Woodman 已提交
159 160 161 162 163 164 165
[setMp3ExtractorFlags]: https://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/extractor/DefaultExtractorsFactory#setMp3ExtractorFlags-int-
[FLAG_DETECT_ACCESS_UNITS]: https://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/extractor/ts/DefaultTsPayloadReaderFactory.html#FLAG_DETECT_ACCESS_UNITS
[FLAG_ALLOW_NON_IDR_KEYFRAMES]: https://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/extractor/ts/DefaultTsPayloadReaderFactory.html#FLAG_ALLOW_NON_IDR_KEYFRAMES
[setTsExtractorFlags]: https://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/extractor/DefaultExtractorsFactory#setTsExtractorFlags-int-
[Wikipedia]: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
[DefaultHttpDataSourceFactory]: http://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/upstream/DefaultHttpDataSourceFactory.html
[isCurrentWindowDynamic]: https://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/ExoPlayer.html#isCurrentWindowDynamic--
O
Oliver Woodman 已提交
166 167 168
[foreground service]: https://developer.android.com/guide/components/services.html#Foreground
[WifiLock]: https://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock.html
[WakeLock]: https://developer.android.com/reference/android/os/PowerManager.WakeLock.html
O
ojw28 已提交
169
[`SDK_INT`]: https://developer.android.com/reference/android/os/Build.VERSION.html#SDK_INT