Last active
August 29, 2015 14:16
-
-
Save triwav/ba72e99e5181427f4645 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
{ | |
} | |
} |
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
Can you explain exactly how you use this to make WebVTT closed caption works? I can get the subtitle to bytes array successfully, then:
to create TextTrackRenderer, but when video plays, subtitle didn't display and there's logcat:
Those lines don't show up when I don't add textRenderer to renderers[ Player.TYPE_TEXT) (video and audio still work).