从后台线程或主 ui 线程使用 exoplayer 更好吗?
Is it better to use exoplayer from a background thread or from the main ui thread?
为避免应用程序出现任何问题,从后台线程使用 exoplayer 还是从主线程使用它更好 ui 好吗?
注意: 当我在主 ui 线程中创建一个 simpleExoPlayer 时:
// 1. Create a default TrackSelector
Handler mainHandler = new Handler();
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector =
new DefaultTrackSelector(videoTrackSelectionFactory);
// 2. Create the player
SimpleExoPlayer player =
ExoPlayerFactory.newSimpleInstance(context, trackSelector);
然后花了 90 毫秒 !!它太慢了,我会有几帧丢失导致我的动画出现抖动。也在做
// Measures bandwidth during playback. Can be null if not required.
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context,
Util.getUserAgent(context, "yourApplicationName"), bandwidthMeter);
// This is the MediaSource representing the media to be played.
MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(mp4VideoUri);
// Prepare the player with the source.
player.prepare(videoSource);
花了大约 20 毫秒!它也很慢,我会再丢一帧
ExoPlayer 的内部负责处理线程 - 因此在主(应用程序)线程上调用大多数 API 是安全的。
It is recommended that ExoPlayer instances are created and accessed from a single application thread. The application's main thread is ideal. Accessing an instance from multiple threads is discouraged, however if an application does wish to do this then it may do so provided that it ensures accesses are synchronized.
除了 Kyle 的解释之外,可以补充的是,除了 release()
调用之外,所有调用都是非阻塞的,因为消息被发送到玩家所在的线程 运行英寸
release()
将阻塞直到资源被释放。仅当您完全销毁播放器实例时才需要调用 release()
。如果你想重用播放器实例,例如。另一个媒体来源调用 stop()
就足够了。
如果 stop
已经被调用,那么所有 release()
将实际做的是阻止任何仍未决的 stop
操作,最后终止播放线程。
为避免应用程序出现任何问题,从后台线程使用 exoplayer 还是从主线程使用它更好 ui 好吗?
注意: 当我在主 ui 线程中创建一个 simpleExoPlayer 时:
// 1. Create a default TrackSelector
Handler mainHandler = new Handler();
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector =
new DefaultTrackSelector(videoTrackSelectionFactory);
// 2. Create the player
SimpleExoPlayer player =
ExoPlayerFactory.newSimpleInstance(context, trackSelector);
然后花了 90 毫秒 !!它太慢了,我会有几帧丢失导致我的动画出现抖动。也在做
// Measures bandwidth during playback. Can be null if not required.
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context,
Util.getUserAgent(context, "yourApplicationName"), bandwidthMeter);
// This is the MediaSource representing the media to be played.
MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(mp4VideoUri);
// Prepare the player with the source.
player.prepare(videoSource);
花了大约 20 毫秒!它也很慢,我会再丢一帧
ExoPlayer 的内部负责处理线程 - 因此在主(应用程序)线程上调用大多数 API 是安全的。
It is recommended that ExoPlayer instances are created and accessed from a single application thread. The application's main thread is ideal. Accessing an instance from multiple threads is discouraged, however if an application does wish to do this then it may do so provided that it ensures accesses are synchronized.
除了 Kyle 的解释之外,可以补充的是,除了 release()
调用之外,所有调用都是非阻塞的,因为消息被发送到玩家所在的线程 运行英寸
release()
将阻塞直到资源被释放。仅当您完全销毁播放器实例时才需要调用 release()
。如果你想重用播放器实例,例如。另一个媒体来源调用 stop()
就足够了。
如果 stop
已经被调用,那么所有 release()
将实际做的是阻止任何仍未决的 stop
操作,最后终止播放线程。