Skip to content

Instantly share code, notes, and snippets.

@triwav
Last active August 29, 2015 14:16
Show Gist options
  • Save triwav/ba72e99e5181427f4645 to your computer and use it in GitHub Desktop.
Save triwav/ba72e99e5181427f4645 to your computer and use it in GitHub Desktop.
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import android.os.AsyncTask;
import android.util.Log;
import com.google.android.exoplayer.FormatHolder;
import com.google.android.exoplayer.SampleHolder;
import com.google.android.exoplayer.SampleSource;
import com.google.android.exoplayer.TrackInfo;
import com.google.android.exoplayer.TrackRenderer;
public class HttpSampleSource extends AsyncTask<Void, Void, Void> implements SampleSource
{
private final String TAG = "HttpSampleSource";
private ByteBuffer byteBuffer;
private int byteBufferSize;
private URL url;
private String mimeType;
private TrackInfo trackInfo;
private boolean readNeeded = true;
HttpSampleSource(URL url, String mimeType)
{
this.url = url;
this.mimeType = mimeType;
trackInfo = new TrackInfo(mimeType, TrackRenderer.MATCH_LONGEST);
}
@Override
public boolean prepare() throws IOException
{
if (getStatus() == AsyncTask.Status.PENDING)
{
execute();
}
return true;
}
@Override
protected Void doInBackground(Void... params)
{
try
{
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1 && !isCancelled())
{
byteBufferSize += len;
byteArrayOS.write(buffer, 0, len);
}
byteBuffer = ByteBuffer.wrap(byteArrayOS.toByteArray());
} catch (Exception e)
{
Debug.warn(TAG, e.getMessage());
}
return null;
}
@Override
public int getTrackCount()
{
return 1;
}
@Override
public TrackInfo getTrackInfo(int track)
{
return trackInfo;
}
@Override
public void enable(int track, long timeUs)
{
readNeeded = true;
}
@Override
public void disable(int track)
{
}
@Override
public void continueBuffering(long playbackPositionUs)
{
}
@Override
public int readData(int track, long playbackPositionUs, FormatHolder formatHolder, SampleHolder sampleHolder, boolean onlyReadDiscontinuity) throws IOException
{
if (byteBuffer == null)
{
return NOTHING_READ;
}
if (sampleHolder != null && readNeeded){
sampleHolder.size = byteBufferSize;
sampleHolder.data = byteBuffer;
readNeeded = false;
return SAMPLE_READ;
}
return END_OF_STREAM;
}
@Override
public long seekToUs(long timeUs)
{
return timeUs;
}
@Override
public long getBufferedPositionUs()
{
return TrackRenderer.END_OF_TRACK;
}
@Override
public void release()
{
}
}
@kientux
Copy link

kientux commented Mar 18, 2015

Can you explain exactly how you use this to make WebVTT closed caption works? I can get the subtitle to bytes array successfully, then:

textRenderer = new TextTrackRenderer(httpSampleSource, player,
                            player.getMainHandler().getLooper(), new WebvttParser());

to create TextTrackRenderer, but when video plays, subtitle didn't display and there's logcat:

W/AudioCapabilities﹕ Unsupported mime audio/x-ms-wma
W/AudioCapabilities﹕ Unsupported mime audio/vnd.rn-realaudio
W/AudioCapabilities﹕ Unsupported mime audio/mpeg-L2
W/AudioCapabilities﹕ Unsupported mime audio/ac3
W/AudioCapabilities﹕ Unsupported mime audio/x-ape
W/AudioCapabilities﹕ Unsupported mime audio/vnd.dts
W/AudioCapabilities﹕ Unsupported mime audio/x-pcm
W/AudioCapabilities﹕ Unsupported mime audio/ffmpeg
W/VideoCapabilities﹕ Unsupported mime video/mpeg2
W/VideoCapabilities﹕ Unsupported mime video/x-ms-wmv
W/VideoCapabilities﹕ Unsupported mime video/vnd.rn-realvideo
W/VideoCapabilities﹕ Unsupported mime video/vc1
W/VideoCapabilities﹕ Unsupported mime video/x-flv
W/VideoCapabilities﹕ Unsupported mime video/divx
W/VideoCapabilities﹕ Unsupported mime video/ffmpeg

Those lines don't show up when I don't add textRenderer to renderers[ Player.TYPE_TEXT) (video and audio still work).

@kientux
Copy link

kientux commented Mar 18, 2015

Sorry, just found out that I forgot to turn on CC :D Your code works very well, thanks :) Still don't know what causes these messages in logcat, but they are harmless.

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