ExoPlayer 2 Миграция
Ниже приведен фрагмент кода, который мы используем для воспроизведения видео в формате mp4 с URL-адреса, используя ExoPlayer1, и я после обновления до ExoPlayer2. Я не уверен, что нужно обновить, чтобы оно работало после обновления, так как я не слишком знаком с самим ExoPlayer и не так уж много полезной документации по этому поводу. Любая помощь приветствуется, если кто-то может затенить немного света. Спасибо, куча
private static final int BUFFER_SEGMENT_SIZE = 64 * 1024;
private static final int BUFFER_SEGMENT_COUNT = 256;
private ExoPlayer videoPlayer;
private Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
private void playVideo(Uri uri) {
Handler handler = new Handler();
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(handler, null);
DataSource dataSource = new DefaultUriDataSource(getContext(), bandwidthMeter, "Android-ExoPlayer");
SampleSource sampleSource = new ExtractorSampleSource(
uri,
dataSource,
allocator,
BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE);
TrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(
sampleSource,
MediaCodecSelector.DEFAULT);
// 2. Construct renderers.
TrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(
getContext(),
sampleSource,
MediaCodecSelector.DEFAULT,
MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT);
// 1. Instantiate the player.
if (videoPlayer == null) {
videoPlayer = ExoPlayer.Factory.newInstance(4);
}
// 3. Inject the renderers through prepare.
videoPlayer.prepare(videoRenderer, audioRenderer);
// 4. Pass the surface to the video renderer.
SurfaceTexture surfaceTexture = textureView.getSurfaceTexture();
Surface surface = new Surface(surfaceTexture);
videoPlayer.sendMessage(videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, surface);
videoPlayer.addListener(new ExoPlayer.Listener() {
@Override
public void onPlayerStateChanged(boolean b, int i) {
if (i == ExoPlayer.STATE_READY) {
mediaControl.setMedia(new MediaControl.Media() {
@Override
public void setCurrentPosition(long positionMs) {
if (videoPlayer != null) {
videoPlayer.seekTo(positionMs);
}
}
@Override
public long getCurrentPosition() {
if (videoPlayer != null) {
return videoPlayer.getCurrentPosition();
}
return 0;
}
@Override
public long getDuration() {
if (videoPlayer != null) {
return videoPlayer.getDuration();
}
return 0;
}
@Override
public void start() {
if (videoPlayer != null) {
videoPlayer.setPlayWhenReady(true);
}
}
@Override
public void pause() {
if (videoPlayer != null) {
videoPlayer.setPlayWhenReady(false);
}
}
@Override
public int getBufferedPercentage() {
if (videoPlayer != null) {
return videoPlayer.getBufferedPercentage();
}
return 0;
}
});
mediaControl.start();
videoPlayer.removeListener(this);
}
}
@Override
public void onPlayWhenReadyCommitted() {
}
@Override
public void onPlayerError(ExoPlaybackException e) {
mediaControl.pause();
}
});
// 5. Start playback.
mediaControl.start();
}
1 ответ
Решение
Handler handler = new Handler();
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(handler, null);
MediaSource sampleSource = new ExtractorMediaSource(
uri,
new DefaultDataSourceFactory(getContext(), "Android-ExoPlayer", bandwidthMeter),
new DefaultExtractorsFactory(), null, null);
if (videoPlayer == null) {
Handler mainHandler = new Handler();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveVideoTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector =
new DefaultTrackSelector(mainHandler, videoTrackSelectionFactory);
LoadControl loadControl = new DefaultLoadControl();
videoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl);
}
videoPlayer.prepare(sampleSource);
videoPlayer.setVideoTextureView(textureView);