Skip to content

Instantly share code, notes, and snippets.

@anotherhale
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save anotherhale/4739df1b6545b5067d76 to your computer and use it in GitHub Desktop.

Select an option

Save anotherhale/4739df1b6545b5067d76 to your computer and use it in GitHub Desktop.
Modifited DefaultRendererBuilder to support external source Uri for TextRenderer.
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer.demo.full.player;
import com.google.android.exoplayer.FrameworkSampleSource;
import com.google.android.exoplayer.MediaCodecAudioTrackRenderer;
import com.google.android.exoplayer.MediaCodecVideoTrackRenderer;
import com.google.android.exoplayer.TrackRenderer;
import com.google.android.exoplayer.demo.full.player.DemoPlayer.RendererBuilder;
import com.google.android.exoplayer.demo.full.player.DemoPlayer.RendererBuilderCallback;
import android.content.Context;
import android.media.MediaCodec;
import android.net.Uri;
import android.widget.TextView;
/**
* A {@link RendererBuilder} for streams that can be read using
* {@link android.media.MediaExtractor}.
*/
public class DefaultRendererBuilder implements RendererBuilder {
private final Context context;
private final Uri uri;
private final Uri textUri;
private final TextView debugTextView;
public DefaultRendererBuilder(Context context, Uri uri, TextView debugTextView) {
this(context, uri, null, debugTextView);
}
public DefaultRendererBuilder(Context context, Uri uri, Uri textUri, TextView debugTextView) {
this.context = context;
this.uri = uri;
this.textUri = textUri;
this.debugTextView = debugTextView;
}
@Override
public void buildRenderers(DemoPlayer player, RendererBuilderCallback callback) {
// Build the video and audio renderers.
FrameworkSampleSource sampleSource = new FrameworkSampleSource(context, uri, null, 2);
// TEXT RENDERER
// FrameworkSampleSource textSource = new FrameworkSampleSource(context, textUri, null, 1);
// Replaced FrameworkSampleSource with ByteArraySampleSource.
// See this gist:
// https://gist.github.com/anotherhale/878c2d87e6347973ef31
ByteArraySampleSource textSource = new ByteArraySampleSource(context, textUri, null, 1);
MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(sampleSource,
null, true, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT, 5000,
player.getMainHandler(), player, 50);
MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource,
null, true, player.getMainHandler(), player);
// Build the debug renderer.
TrackRenderer debugRenderer = debugTextView != null
? new DebugTrackRenderer(debugTextView, videoRenderer)
: null;
// Build the subtitle renderer
TrackRenderer subtitleRenderer = null;
if (subtitleUri != null) {
subtitleRenderer = new TextTrackRenderer(textSource, new TtmlParser(), player, player.getMainHandler().getLooper());
}
// Invoke the callback.
TrackRenderer[] renderers = new TrackRenderer[DemoPlayer.RENDERER_COUNT];
renderers[DemoPlayer.TYPE_VIDEO] = videoRenderer;
renderers[DemoPlayer.TYPE_AUDIO] = audioRenderer;
renderers[DemoPlayer.TYPE_TEXT] = subtitleRenderer;
renderers[DemoPlayer.TYPE_DEBUG] = debugRenderer;
callback.onRenderers(null, null, renderers);
}
}
@Kening
Copy link

Kening commented Nov 14, 2014

are you able to get closed caption working this way? I am getting this error. It seems we can't just pass a non-media url to MediaExtractor's setDataSource, in my case, it's a dfxp file on a remote server.

Internal track renderer error.
com.google.android.exoplayer.ExoPlaybackException: java.io.IOException: Failed to instantiate extractor.
at com.google.android.exoplayer.MediaCodecTrackRenderer.doPrepare(MediaCodecTrackRenderer.java:190)
at com.google.android.exoplayer.TrackRenderer.prepare(TrackRenderer.java:115)
at com.google.android.exoplayer.ExoPlayerImplInternal.incrementalPrepareInternal(ExoPlayerImplInternal.java:267)
at com.google.android.exoplayer.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:195)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.google.android.exoplayer.ExoPlayerImplInternal$1.run(ExoPlayerImplInternal.java:109)
Caused by: java.io.IOException: Failed to instantiate extractor.
at android.media.MediaExtractor.setDataSource(Native Method)
at android.media.MediaExtractor.setDataSource(MediaExtractor.java:140)
at android.media.MediaExtractor.setDataSource(MediaExtractor.java:115)
at com.google.android.exoplayer.FrameworkSampleSource.prepare(FrameworkSampleSource.java:68)
at com.google.android.exoplayer.MediaCodecTrackRenderer.doPrepare(MediaCodecTrackRenderer.java:185)
            at com.google.android.exoplayer.TrackRenderer.prepare(TrackRenderer.java:115)
            at com.google.android.exoplayer.ExoPlayerImplInternal.incrementalPrepareInternal(ExoPlayerImplInternal.java:267)
            at com.google.android.exoplayer.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:195)
            at android.os.Handler.dispatchMessage(Handler.java:98)
            at android.os.Looper.loop(Looper.java:136)
            at android.os.HandlerThread.run(HandlerThread.java:61)
            at com.google.android.exoplayer.ExoPlayerImplInternal$1.run(ExoPlayerImplInternal.java:109)

@anotherhale
Copy link
Author

I have updated the code above with the changes I made in my project to make it work for local TTML files.

// TEXT RENDERER
// FrameworkSampleSource textSource = new FrameworkSampleSource(context, textUri, null, 1);
// Replaced FrameworkSampleSource with ByteArraySampleSource.  
// See this gist:
// https://gist.github.com/anotherhale/878c2d87e6347973ef31
ByteArraySampleSource textSource = new ByteArraySampleSource(context, textUri, null, 1);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment